0913 作业

0912作业

猜年龄游戏

给定年龄,用户可以猜三次年龄

年龄猜对,让用户选择两次奖励

用户选择两次奖励后可以退出

age=18
count=0
jiang_li={1:'机加工',2:'下料处',3:'磨具制造',4:'焊接操作'}
while count<3:
    inp_age=int(input('请输入年龄:'))
    if inp_age < age:
        print('猜小了')
    elif inp_age>age:
        print('猜大了')
    else:
        print('猜对了!请选择下列奖品并输入序列号')
        print(jiang_li)
        # select=int(input('请选择第一个奖品:'))
        xuanze=0
        while xuanze <2:
            select = int(input('请选择奖品:'))
            for i in jiang_li:
                if select == i:
                    print('恭喜您获得:', jiang_li[i])
            xuanze += 1
        break
    count+=1
print('游戏结束')

三级菜单

  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

# part1(初步实现):能够一层一层进入
layers = [
    menu,
]

while True:
    current_layer = layers[-1]
    for key in current_layer:
        print(key)

    choice = input('>>: ').strip()
    
    if choice == 'q':
        break

    if choice not in current_layer: continue

    layers.append(current_layer[choice])

# part2(改进):加上退出机制
layers = [
    menu,
]

while True:
    if len(layers) == 0: break
    current_layer = layers[-1]
    for key in current_layer:
        print(key)

    choice = input('>>: ').strip()

    if choice == 'b':
        layers.pop(-1)
        continue
    if choice == 'q': break

    if choice not in current_layer: continue

    layers.append(current_layer[choice])
原文地址:https://www.cnblogs.com/fwzzz/p/11523637.html