基础之实战猜年龄游戏

'''
1.给定年龄,用户可以猜三次年龄
2.年龄猜对,让用户选择两次奖励
3.用户选择两次奖励后可以退出
'''

age=18
count=0
prize={
    '0':'光辉岁月',
    '1':'有道词典',
    '3':'布娃娃',
    '4':'钢铁是怎样放弃的',

}
while count<3:
    choice_age=input('请输入你要猜的年龄,退出请输入q:').strip()
    if choice_age=='q':
        break
    if not choice_age.isdigit():
        print('必须输入数字:')
        continue
    choice_age=int(choice_age)
    if choice_age==age:
        print('恭喜你猜对了:')
        print(prize)
        for i in range(2):
            choice = input('请输入你要选择的奖品或q退出:').strip()
            if choice == 'q':
                break
            if not choice.isdigit():
                print('请必须输入数字:')
            if choice in prize:
                print(f'得到的奖品是:{prize[choice]}')
            else:
                print('请重新输入')
                continue
        break
    elif choice_age>age:
        print('猜大了')
    else:
        print('猜小了')
    count+=1
    if count==3:
        choice2=input('请选择继续Y/y或任意键退出:').strip()
        if choice2=='Y' or choice2=='y':
            count=0
        else:
            break





原文地址:https://www.cnblogs.com/jinhongquan/p/11514802.html