字典--四级菜单

Posted leejay

tags:

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

实现功能:

1、输入父菜单进入到子菜单,以此类推

2、可返回上一层菜单

privince_info = {
    "河南": {
        "信阳市": {
            "商城县": ["双椿铺镇", "河凤桥镇", "三里坪"],
            "潢川县": ["白店乡", "卜塔集镇"]
        },
        "驻马店": {
            "确山县": ["李新店镇", "刘店镇"],
            "泌阳县": ["二铺村", "付庄乡"]
        }
    },
    "湖北": {
        "武汉市": {
            "江岸区": ["黄冈", "孝感"],
            "武昌区": ["翠柳村", "梨园村"]
        }
    }
}
print("返回上一级请输入‘b‘; 输入‘q‘退出程序")


while True:
    for print_privince in privince_info:
        print(print_privince)
    choice_privince = input("请选择省份:")
    if choice_privince == b or choice_privince == B:  #输入b返回上一级
        break
    elif choice_privince == q or choice_privince == Q:  #输入q退出程序
        quit()
    if choice_privince not in privince_info:  #如果输入的省份不再字典中,重新输入
        continue
    # for privince in privince_info[choice_privince]:
    #     print(">> " + privince)
    while True:
        for privince in privince_info[choice_privince]:
            print(">> " + privince)
        choice_city = input("请选择城市:")
        if choice_city == b or choice_city == B:  #输入b返回上一级
            break
        elif choice_city == q or choice_city == Q:
            quit()
        if choice_city not in privince_info[choice_privince]:  #如果输入的城市不再字典中,重新输入
            continue

        while True:
            for city in privince_info[choice_privince][choice_city]:
                print(">>>> " + city)
            choice_town = input("请选择城镇:")
            if choice_town == b or choice_town == B:  #输入b返回上一级
                break
            elif choice_town == q or choice_town == Q:
                quit()
            if choice_town not in privince_info[choice_privince][choice_city]: #如果输入的城镇不再字典中,重新输入
                continue

            while True:
                for town in privince_info[choice_privince][choice_city][choice_town]:
                    print(">>>>>> " + town)
                choice_cun = input("输入‘b‘返回; 输入‘q‘退出:")
                if choice_cun == b or choice_cun == B:
                    break
                elif choice_cun == q or choice_cun == Q:
                    quit()
                if choice_cun in privince_info[choice_privince][choice_city][choice_town]: #输入最后一级什么不执行,防止报错
                    pass

 

以上是关于字典--四级菜单的主要内容,如果未能解决你的问题,请参考以下文章

四级菜单实现(Python)

13 个非常有用的 Python 代码片段

添加片段时的 FlyOut 菜单设计问题

创建片段的新实例时菜单未膨胀

Python snippet(代码片段)

Python代码阅读(第19篇):合并多个字典