示例:input
age_of_oldboy = 56it
count = 0 while True:循环
if count == 3:im
#只能尝试三次db
breakdi
guess_age = int(input("guess age:"))ant
if guess_age == age_of_oldboy :while
print("yes,you got it.")co
break # 猜对就跳出循环time
elif guess_age > age_of_oldboy:
print("think smaller...") #给第二种可能,不是就有可能
else:
print("think bigger!")
count += 1
示例2:
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 keep guessing?")#这4行是能够询问是否继续
if countine_confirm != 'n':
count = 0
else:
print("you have tried too many times..")