python学习---while循环

while循环的基本用法:

count = 0
while True:
    print("count",count)
    count= count+1   # count +=1

while与if结合的用法:

count = 0  //用来计数
age_of_oldboy = 56

while count < 3:  # 只有3次机会,否则就退出
    guess_age = int(input("guess_age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it")
        break    # 表示猜对了就退出
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger...")
    count +=1
else:
    print("you have tried too many times....")

运行结果,如图

原文地址:https://www.cnblogs.com/ommph/p/11377622.html