python的while循环

Posted 小短腿电工

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("超时,一会再试")

 

以上是关于python的while循环的主要内容,如果未能解决你的问题,请参考以下文章

使用While循环导出Excel

常用python日期日志获取内容循环的代码片段

python的while循环的学习

python学习八(while循环)

Python3基础12——while循环

Python 图中的while循环改for循环,怎么改,直接回答代码,谢谢