Python实例---三级菜单的实现[high]

Posted 小a玖拾柒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实例---三级菜单的实现[high]相关的知识,希望对你有一定的参考价值。

# version: python3.2.5
# author: ‘FTL1012‘
# time: 2017/12/7 09:16
menu = {
    ‘陕西‘: {
        ‘西安‘: {
            ‘未名区‘: [‘国美‘, ‘苏宁‘, ‘京东‘],
            ‘无名区‘: [‘百度‘, ‘360 ‘, ‘搜狗‘],
            ‘优雅区‘: [‘腾讯‘, ‘默默‘, ‘订订‘]
        },
        ‘宝鸡‘: {
            ‘进队区‘: [‘word‘, ‘excel‘, ‘ppt‘],
            ‘挖第区‘: [‘华为‘, ‘锤子‘, ‘小米‘],
            ‘阿克区‘: [‘adidas‘, ‘unique‘, ‘masterbrown‘]
        },
        ‘汉中‘: {
            ‘呵呵区‘: [‘流浪‘, ‘excel‘, ‘ppt‘],
            ‘幻化区‘: [‘猥琐‘, ‘发育‘, ‘别浪‘],
            ‘三只区‘: [‘后裔‘, ‘李白‘, ‘杜甫‘]
        },
    },
    ‘山东‘: {
        ‘山东市‘: {
            ‘惠普区‘: [‘华夏‘, ‘建设‘, ‘农行‘]
        }
    },
    ‘湖南‘: {
        ‘长沙‘: {
            ‘豆腐区‘: [‘键盘‘, ‘鼠标‘, ‘显示器‘]
        }
    }
}

current_layer = menu # 用于动态循环
parent_lists = []  # 保存父级,最后一个元素永远都是父亲级别
while True:
    if len(current_layer) != 0:
        for k in current_layer:
            print(k)
    else:
        print("no")
    choice = input("请选择:").strip()
    if 0 == len(choice):
        continue
    if choice in current_layer:
        # parent_layer = current_layer
        parent_lists.append(current_layer)
        current_layer = current_layer[choice]
    elif choice == ‘b‘:
        print("返回上一级目录...")
        # for k in parent_layer:
        #     print(k)
        # current_layer = parent_layer 
        if parent_lists:
            current_layer = parent_lists.pop()     # pop会取出并删除最后一个元素
        else:
            print("已经到达首页啦...")
    else:
        print("输入错误...")

以上是关于Python实例---三级菜单的实现[high]的主要内容,如果未能解决你的问题,请参考以下文章

(转)Python字典实现三级菜单

python学习之路 三级菜单的实现

python实例_三级菜单

Python 实例3—三级菜单

Python学习笔记五:字符串常用操作,字典,三级菜单实例

python实现三级菜单