码迷,mamicode.com
首页 > 其他好文 > 详细

字典--四级菜单

时间:2019-12-07 21:10:19      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:河南   color   执行   字典   省份   --   功能   bre   bsp   

实现功能:

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

 

字典--四级菜单

标签:河南   color   执行   字典   省份   --   功能   bre   bsp   

原文地址:https://www.cnblogs.com/leejay/p/12003284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!