python的while循环

 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("超时,一会再试")
原文地址:https://www.cnblogs.com/haozheyu/p/8076327.html