學習要點:
1.第一個改進要求:猜錯的時候程序提示用戶當前的輸入比答案大了還是小了。
2.第二個改進要求:程序應該提供多次機會給用戶猜測,專業點來說就是程序需要重複運行某些代碼。
3.第三個改進要求:每次運行程序產生的答案是隨機的:(引入模塊random模塊)
這個random模塊裏邊有個函數叫做:randint(),ta會返回一個隨機的整數。
if 條件:
條件爲真(true)執行的操作。
else:
條件爲假(False)執行的操作。
python的while循環語法:
while 條件 :
條件爲真(True)執行的操作。
學習代碼如下:
import random
secret = random.randint(1,20)
print('---------------------和你玩個小遊戲---------------------------')
temp = input("請猜猜我心中的數字:")
guess = int(temp)
while guess != secret:
temp = input("hi呀,你猜錯了,相信你可以的繼續猜:")
guess = int(temp)
if guess == secret:
print("嗨呀,難道你是我肚子一條蛔蟲嗎")
print("哈哈,猜對也沒有獎勵。")
else:
if guess > secret:
print("不好意思,您猜的數字大了哈")
else:
if 1 <= guess < 21:
print('你猜的數字很接近,繼續加油。')
else:
print("不好意思,您猜的數字小了哈")
print("遊戲結束,不玩啦")
005-Python的數據類型。
init() float() str() e記法
例如:
>> a = 0.000000000008
>> a
8e-12
>> a = 9e10
>> a
90000000000.0
>>
type() isinstance()
例題:
>> a = '520'
>> type(a)
<class 'str'>
>> a = 520
>> type (a)
<class 'int'>>> isinstance(a,int)
True>> isinstance(a,float)
False
>>
測試題:
-
Python 的 floor 除法現在使用 “//” 實現,那 3.0 // 2.0 您目測會顯示什麼內容呢?
地板除法,3//2=1, 3.0//2.0=1.0,整數的地板除//永遠是整數,即使除不盡
-
a < b < c 事實上是等於?
a<b and b<c
- 不使用 IDLE,你可以輕鬆說出 5 ** -2 的值嗎?
1/25,冪級運算優先級高於其他算術操作符合邏輯操作符,但是冪級運算有一點比較特殊
冪運算操作符比其左側的一元操作符優先級高,比其右側的一元操作符優先級低。
例:-52=-25,是-(52),而不是(-5)**2
>> -52
-25
>> 5-2
0.04
>>
-
如何簡單判斷一個數是奇數還是偶數?
取餘%, if(a%2) "是奇數" else "是偶數" - 請用最快速度說出答案:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
not
1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)
== 0 or 0 or 4 or 6 or 9
== 4
例如:
>> 3 or 5
3
>> 3 and 5
5
>> 0 and 5
0
>> 0 or 5
5
>>
- 還記得我們上節課那個求閏年的作業嗎?如果還沒有學到“求餘”操作,還記得用什麼方法可以“委
曲求全”代替“%”的功能呢?
print("-----------------------還記得我們上節課那個求閏年的作業嗎?如果還沒有學到“求餘”操
作,還記得用什麼方法可以“委曲求全”代替“%”的功能呢?---------------------------------")
years=input("請輸入一個年數判斷是否閏年:")
year=int(years)
a=year/4
b=year-4*a
if (b==0):
print("閏年")
else:
print("不是閏年")
- 請寫一個程序打印出 0~100 所有的奇數。
for i in range(1,100,2):
print(i)
i=1
while i <= 100:
if (i % 2) != 0:
print(i)
i+=1
else:
print('end')