Python攻关之字典简单实现三级菜单

#__author:"Zph"
#date: 2019/12/2


# 三级菜单:
# 1.可以一层一层的进入到所有层
# 2.可以在每层返回上一层
# 3.可以在任何层退出主菜单




menu = {
    '北京':{
        '朝阳':{
            '国贸':{
                "CICC":{},
                "BP":{},
                "人民银行":{},
                "CCTV":{},
            },
            '望京':{
                "陌陌":{},
                "奔驰":{},
                "360":{},
            },
            '三里屯':{
                "优衣库":{},
                "Adidas":{},
            },
        },
        '昌平':{
            '沙河':{
                "老男孩":{},
                "包子":{},
            },
            '天通苑':{
                "我爱我家":{},
                "链家":{},
            },
            '回龙观':{},
        },
        '海淀':{
            '五道口':{
                "快手":{},
                "网易":{},
                "sohu":{},
                "sogo":{},
            },
            '中关村':{
                "youku":{},
                "aiqiyi":{},
                "汽车之家":{},
                "新东方":{},
                "qq":{},
            },
        },

    },
    '上海':{
        '浦东':{
            '陆家嘴':{
                "陆家嘴":{},
                "CICC":{},
            },
            "外滩":{},
        },
        '虹桥':{},
        '闵行':{},
    },
    '陕西':{
        '西安':{
            "长安区":{},
            "雁塔区":{},
            "曲江新区":{},
        },
        '渭南':{
            "蒲城":{},
            "大荔":{},
        },
        '华阴':{},
    },
}
back_flag = False
exit_flag = False
while not back_flag and not exit_flag:
    for key in menu:
        print(key)
    choice = input("1>>:").strip()
    if choice == 'q':
        exit_flag = True
    if choice in menu:
        while  not back_flag and not exit_flag :  #让程序停留在这一层
            for key2 in menu[choice]:
                print(key2)
            choice2 = input("2>>:").strip()
            if choice2 == 'q':
                back_flag = True
            if choice2 == 'b':
                exit_flag = True
            if choice2 in menu[choice]:
                while not back_flag and not exit_flag:
                    for key3 in menu[choice][choice2]:
                        print(key3)
                    choice3 = input("3>>:").strip()
                    if choice3 == 'q':
                        back_flag = True
                    if choice3 == 'b':
                        exit_flag = True
                    if choice3 in menu[choice][choice2]:
                        while not back_flag and not exit_flag:
                            for key4 in menu[choice][choice2][choice3]:
                                print(key4)
                            choice4 = input("4>>:").strip()
                            print("最后一页")
                            if choice4 == 'q':
                                back_flag = True
                            if choice4 == 'b':
                                exit_flag = True
                        else:
                            back_flag = False
                else:
                    back_flag = False
        else:
            back_flag = False





#改进版本
menu = {
    '北京':{
        '朝阳':{
            '国贸':{
                "CICC":{},
                "BP":{},
                "人民银行":{},
                "CCTV":{},
            },
            '望京':{
                "陌陌":{},
                "奔驰":{},
                "360":{},
            },
            '三里屯':{
                "优衣库":{},
                "Adidas":{},
            },
        },
        '昌平':{
            '沙河':{
                "老男孩":{},
                "包子":{},
            },
            '天通苑':{
                "我爱我家":{},
                "链家":{},
            },
            '回龙观':{},
        },
        '海淀':{
            '五道口':{
                "快手":{},
                "网易":{},
                "sohu":{},
                "sogo":{},
            },
            '中关村':{
                "youku":{},
                "aiqiyi":{},
                "汽车之家":{},
                "新东方":{},
                "qq":{},
            },
        },

    },
    '上海':{
        '浦东':{
            '陆家嘴':{
                "陆家嘴":{},
                "CICC":{},
            },
            "外滩":{},
        },
        '虹桥':{},
        '闵行':{},
    },
    '陕西':{
        '西安':{
            "长安区":{},
            "雁塔区":{},
            "曲江新区":{},
        },
        '渭南':{
            "蒲城":{},
            "大荔":{},
        },
        '华阴':{},
    },
}

current_layer = menu   #实现动态循环
parent_layers = []    #列表保存所有父级,最后一个元素永远都是父亲级
while True:
    for key in current_layer:
        print(key)
    choice = input(">>>:").strip()
    if len(choice) == 0:
        continue
    if choice in current_layer:
        parent_layers.append(current_layer)   #在进入下一级之前,把当前层的父亲级追加到列表中,
        #下一次 使用b时 直接用列表取出最后一个父亲级 就是上一层
        current_layer = current_layer[choice]   #改成了子层
    elif choice == 'b':
        if parent_layers:
            current_layer = parent_layers.pop()  #返回列表的最后一个值
    else:
        print("无此项")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值