Day 06 jobs

Python basis of real guessing game

  1. The given number, the user can guess the age of three
  2. Digital guess, allowing users to select the award twice
  3. After selecting the user can exit incentives
import time

print('-' * 20, '猜数字小游戏,答对赢取超级大礼!', '-' * 20)
time.sleep(1)

print('游戏开始!你一共有三次机会哦')
time.sleep(1)

num = 18
reward_dict = {1: '马云', 2: '雷军', 3: '马化腾'}
count = 0

while True:
    num_inp = input('请输入你猜的数字: ')
    if num_inp.isdigit() is False:
        print('输入无效')
        continue
    num_inp = int(num_inp)
    if num_inp > num:
        print('猜大了')
    elif num_inp < num:
        print('猜小了')
    else:
        print('猜对了')
        print(f'快选择你的奖品吧:{reward_dict}')
        choice = input('输入编号以选择奖品(奖品不喜欢输入其他任意内容退出): ')
        if choice in ['1', '2', '3']:
            print(f'喜提{reward_dict[int(choice)]}一只, 快去前台领奖吧!')
        break

    count += 1
    if count == 3:
        break

print('游戏结束')

Advanced Python combat the three-level menu

  1. Print provincial, municipal and county levels menu
  2. Go back one level
  3. You can exit the program at any time
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

tag = True
while tag:
    menu1 = menu
    for key in menu1:
        print(key)

    choice1 = input('第一层>>> ').strip()

    if choice1 == 'b':
        break
    if choice1 == 'q':
        tag = False
        continue
    if choice1 not in menu1:
        continue

    while tag:
        menu2 = menu1[choice1]
        for key in menu2:
            print(key)

        choice2 = input('第二层>>>: ').strip()
        if choice2 == 'b':
            break
        if choice2 == 'q':
            tag = False
            continue
        if choice2 not in menu2:
            continue

        while tag:
            menu3 = menu2[choice2]
            for key in menu3:
                print(key)

            choice3 = input('第三层>>>: ')
            if choice3 == 'b':
                break
            if choice3 == 'q':
                tag = False
                continue
            if choice3 not in menu3:
                continue

            while tag:
                menu4 = menu3[choice3]
                for key in menu4:
                    print(key)

                choice4 = input('第四层>>>: ')
                if choice4 == 'b':
                    break
                if choice4 == 'q':
                    tag = False
                    continue
                if choice4 not in menu3:
                    continue

Guess you like

Origin www.cnblogs.com/bigb/p/11514986.html