1. 程式人生 > >python的while循環

python的while循環

color for pos while post inpu python pytho got

 1 age_of_laochuanzhang = 56
 2 conut = 0
 3 while True:
 4     if  conut == 3:
 5         print("輸入次數上限")
 6         break
 7     age = int(input("請輸入你的年齡:"))
 8     if age_of_laochuanzhang == age :
 9         print("yes, you got it.")
10         break
11     elif age_of_laochuanzhang > age:
12         print
("think smaller...") 13 else: 14 print("think bigger!") 15 conut +=1

第一版,

優化後的

age_of_laochuanzhang = 56
conut = 0
while conut< 5:
    age = int(input("請輸入你的年齡:"))
    if age_of_laochuanzhang == age :
        print("yes, you got it.")
        break
    elif age_of_laochuanzhang > age:
        
print("think smaller...") else: print("think bigger!") conut +=1 print("超時,一會再試")

age_of_laochuanzhang = 56

for i in range(5):
    age = int(input("請輸入你的年齡:"))
    if age_of_laochuanzhang == age :
        print("yes, you got it.")
        break
    elif age_of_laochuanzhang > age:
        
print("think smaller...") else: print("think bigger!") print("超時,一會再試")

python的while循環