完善的猜年龄

import random
age = random.randint(1,99)
count = 0
tag = True
box_dict = {
    1:'娃娃',
    2:'金箍棒',
    3:'电动车'
}
# print(age)
while tag:
    inp_name = input('请输入一个数字:>>>').strip()
    if inp_name.isdigit():
        inp_name = int(inp_name)
        if inp_name > age:
            print('猜大了')
            count += 1
        elif inp_name < age:
            print('猜小了')
            count += 1
        else:
            while True:
                for x,y in box_dict.items():
                    print(f'编号:{x}   礼物:{y}')
                box = int(input('恭喜您,猜对了!请选择你想要的礼物').strip())
                if box >= 0 and box <= len(box_dict):
                    print(f'恭喜您获得{box_dict[box]}')
                    tag = False
                    break
                else:
                    print('请输入有效编号!')
    else:
        print(f'你的年龄真的是{inp_name}')
        continue
    if  count == 3:
        while True:
            again = input('是否继续').strip()
            if again == 'y':
                count = 0
                break
            elif again == 'n':
                tag = False
                break
            else:
                print('字都不会输入,重来!')
原文地址:https://www.cnblogs.com/xiongchao0823/p/11288934.html