python while基础实例

1、循环十次退出while循环:

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

2、猜年纪:
age_of_oldboy = 56

#guess_age = int(input("guess age:"))

count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("yes, you got it. ")
break #跳出循环就不走else
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count+=1
else:
print("you have tried too many times.. ")

3、达到3次,提示是否继续竞猜,输入n退出:
count = 0
while count<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
if count == 3:
countine_confirm = input("do you want to keep guessing..?")
if countine_confirm != 'n':
count = 0

猜你喜欢

转载自www.cnblogs.com/wzsx/p/8932899.html