Python basis of the actual age of guessing game

Python basis of the actual age of guessing game

  1. Given the age of the user can guess the age of three

  2. Age guessed, allowing users to select the award twice

  3. Users can select two bonus quit

age = 18  # 答案
count = 0  # 游戏次数控制
prize_dict = {0: '布娃娃', 1: '变形金刚', 2: '奥特曼', 3: '<Python从入门到放弃>'}

# 核心代码
while count < 3:
    inp_age = input('请输入你的年龄>>>')  # 与用户交互

    # 判断用户是否骚扰(超纲:判断用户输入的是否为数字)
    if not inp_age.isdigit():
        print('傻逼,你的年龄输错了')
        continue

    inp_age_int = int(inp_age)

    # 核心逻辑,判断年龄
    if inp_age_int == age:
        print('猜对了')

        print(prize_dict)  # 打印奖品

        # 获取两次奖品
        for i in range(2):
            prize_choice = input(
                '请输入你想要的奖品,如果不想要,则输入"n"退出!!!')  # 与用户交互获取奖品

            # 判断是否需要奖品
            if prize_choice != 'n':
                print(f'恭喜你获得奖品: {prize_dict[int(prize_choice)]}')
            else:
                break
        break

    elif inp_age_int < age:
        print('猜小了')

    else:
        print('猜大了')

    count += 1  # 成功玩一次游戏

    if count != 3:
        continue

    again_choice = input('是否继续游戏,继续请输入"Y",否则任意键直接退出.')  # 交互是否再一次

    # 判断是否继续
    if again_choice == 'Y':
        count = 0
请输入你的年龄>>>18
猜对了
{0: '布娃娃', 1: '变形金刚', 2: '奥特曼', 3: '<Python从入门到放弃>'}
请输入你想要的奖品,如果不想要,则输入"n"退出!!!0
恭喜你获得奖品: 布娃娃
请输入你想要的奖品,如果不想要,则输入"n"退出!!!1
恭喜你获得奖品: 变形金刚

Guess you like

Origin www.cnblogs.com/nickchen121/p/11069989.html