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("输入错误...")
原文地址:https://www.cnblogs.com/ftl1012/p/9382390.html