三级菜单

打印三级菜单


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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞