例1
count=0
while True:
print("count:",count)
count=count+1 // count+=1
输出结果:
count: 230033
count: 230034
count: 230035
count: 230036
count: 230037
count: 230038
count: 230039
count: 230040
例2:
age_of_oldboy=56
count=0
while True:
if count==3 :
break
guess_age=int(input("guess_age:"))
if age_of_oldboy==guess_age:
print("yes,you got it")
break
elif age_of_oldboy>guess_age:
print("think smaller")
else:
print("think older")
count+=1
输出结果:
guess_age:56
yes,you got it
例3:
age_of_oldboy=56
count=0
while count<3 : //不要忘记:,在这里直接隐含着等于3的时候break,执行了3次
guess_age=int(input("guess_age:"))
if age_of_oldboy==guess_age:
print("yes,you got it")
break
elif age_of_oldboy>guess_age:
print("think smaller")
else:
print("think older")
count+=1
输出结果:
guess_age:1
think smaller
guess_age:58
think older
guess_age:56
yes,you got it
例4:
age_of_oldboy=56
count=0
while count<3 :
guess_age=int(input("guess_age:"))
if age_of_oldboy==guess_age:
print("yes,you got it")
break
elif age_of_oldboy>guess_age:
print("think smaller")
else:
print("think older")
count+=1
else:
print("you have tried too many times!") //如果while的条件符合 就输出while里面的,如果不符合就输出you have tried too many times!
输出结果:
guess_age:1
think smaller
guess_age:2
think smaller
guess_age:3
think smaller
you have tried too many times!
输出结果:
guess_age:1
think smaller
guess_age:56
yes,you got it //没有输出you have tried too many times!
原文:https://www.cnblogs.com/googlewang/p/10665425.html