方法1:
# first method
age_of_oldboy = 56
count =int(input("guess count:")) #设置猜的次数
while count>0:
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
方法2:
# second method
age_of_oldboy = 56
count=0
#while true:
while count<3:
# if count == 3:
# break
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: #如果while成立,执行以下代码。否则,执行else后代码
print("you have tried too many times,fuck off!")
方法3之升级版:
# author:Iron Qi
age_of_oldboy = 56
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