码迷,mamicode.com
首页 > 编程语言 > 详细

python的while循环

时间:2017-12-21 00:20:48      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:zha   blog   int   got   break   color   次数   pre   you   

 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循环

标签:zha   blog   int   got   break   color   次数   pre   you   

原文地址:http://www.cnblogs.com/haozheyu/p/8076327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!