while循环写猜年龄及优化版,任性玩版

增加:1、循环猜,2、最多猜3次,3、超过3次输出猜次数过多:

age_of_XueKe=18

count=0
while True:
if count==3:
break
age = int(input("age:"))
if age == age_of_XueKe:
print("Yes,you got it!")
break
elif age >age_of_XueKe:
print("Think smaller!")
else:
print("Think bigger!")
count=count+1
if count==3:
print("You have tried too many times...")

优化版:

age_of_XueKe=18

count=0
while count<3:
age = int(input("age:"))
if age == age_of_XueKe:
print("Yes,you got it!")
break
elif age >age_of_XueKe:
print("Think smaller!")
else:
print("Think bigger!")
count=count+1
else:
print("You have tried too many times...")

任性玩(3次过后询问是否继续):
age_of_XueKe=18

count=0
while count<3:
age = int(input("age:"))
if age == age_of_XueKe:
print("Yes,you got it!")
break
elif age >age_of_XueKe:
print("Think smaller!")
else:
print("Think bigger!")
count=count+1
if count==3:
continue_confirm=input("Do you want to keep guessing...?")
if continue_confirm !="n": # != → 不等于
count=0 #计数重置,重复上面循环
else:
print("You have tried too many times...")

猜你喜欢

转载自www.cnblogs.com/shmilymima/p/9265574.html