需求:
年龄是1到99的随机数,如果猜三次以后还没猜对,提示:输入‘Y’继续,否则退出
代码:
import random
age = random.randint(1,99) count = 0 while 1: guess = input('猜一猜年龄:') if guess.isdigit(): #输入必须是数字 count+=1 if count %3 ==0: sure = str(input('是否继续?')) if sure == 'Y': continue else: print('游戏结束') break if guess == age: print('恭喜你猜对啦!') break else: print('请输入数字!') break