python 猜年龄游戏(流程控制)

'''
猜年龄游戏,有三次机会猜,三次后,若没有猜对,则输入是否想继续,如是y/Y则继续,否则就结束游戏。判断所输入的字符串是否为数字。
'''
import random
n = random.randint(20,30)
count = 0
while count < 3:
guss_year_str = input("请猜一个年龄:")
if guss_year_str.isdigit():
#用于判断所输入的字符串是不是数字
guss_year = int(guss_year_str)
if n > guss_year:
print("猜小了!")
elif n < guss_year:
print("猜大了!")
else:
print("恭喜你!")
break
count +=1
if count ==3:
choice = input("是否想再猜(请输入Y/N):")
if choice == "Y" or choice == "y":
count =0
else:
print("游戏结束!")
else:
print("请输入数字。")
posted @ 2020-09-21 21:38  忆浩铭  阅读(462)  评论(0编辑  收藏  举报