首页 > 其他 > 详细

第12节 while循环

时间:2019-04-07 15:16:15      阅读:115      评论:0      收藏:0      [点我收藏+]

例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!

第12节 while循环

原文:https://www.cnblogs.com/googlewang/p/10665425.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!