首页 > 其他 > 详细

三级菜单

时间:2019-10-17 01:31:45      阅读:73      评论:0      收藏:0      [点我收藏+]
打印三级菜单

menu = {
    ‘北京‘:{
        ‘朝阳‘:{},
        ‘望京‘:{},
        ‘三里屯‘:{},

        ‘昌平‘:{},
        ‘海锭‘:{
            "五道口":{
                "谷歌":{},
                "网易":{},
                "快手":{},
            }
        }
    },
    ‘上海‘:{},
}

current_layer = menu        #实现动态循环

parent_list = []            #保存所有父级,最后一个元素永远是父级
while True:
    for key in current_layer:
        print(key)
    choice = input(">>>").strip()   #.strip去表符、空格
    if len(choice) == 0:continue    #判断是否为空
    if choice in current_layer:

        parent_list.append(current_layer)   #在进入下一层前,把当前层追加到列表里
        current_layer = current_layer[choice]   #改为子层
    elif choice == "b":
        if parent_list:
            current_layer = parent_list.pop()   #删除列表里最后一个元素并返回给变量
    elif choice == "q":
        break

    else:
        print("没有此选项,请重新选择")

输出结果:

技术分享图片

三级菜单

原文:https://blog.51cto.com/13528668/2443057

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!