[Python3]三级菜单

本来呢,我自己写了一个版本,就是一个特别low的每一层都套一个while循环来实现三级菜单。

后来看了alex老师的视频,学到了一个新的更高效的版本。所以我还是只粘上高效版好了,我的那个版本应该就是大众的常规思路了= = 这个是我根据老师的思路写的,可能有些许不一样。功能是没问题的。这个例题只是仓促间完成功能需求,细节需要再进行优化。

需求:
可依次选择进入各子菜单
可从任意一层往回退到上一层
可从任意一层退出程序

# _author_:wyq
# _date_:2018/7/14
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车站': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}

current_layer = menu
layers = []

while True:
    for i in current_layer:
        print(i)
    choice = input('>>>:')
    if not choice:
        continue
    elif choice in current_layer:
        layers.append(current_layer)
        current_layer = current_layer[choice]
    elif choice == 'b':
        if len(layers) != 0:
            current_layer = layers.pop()
        else:
            print('已经是最顶层')
    elif choice == 'q':
        exit()



猜你喜欢

转载自blog.csdn.net/TynMhxx/article/details/81044701
今日推荐