字典--四级菜单

实现功能:

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

 

上一篇:poj2431Expedition


下一篇:寒假特训——I - Fair