字典应用三级城市菜单的查询
多级菜单
·三级
·可依次选择进入各子菜单
·所需新知识点:列表、字典
# Author:Qiang
city_dict = {'LiaoNing': {'ShenYang': ['DaDong', 'HuangGu', 'ShenBei', 'HunNan'],
'LiaoYang': ['BaiTa', 'DengTa', 'GongChangLing', 'WenSheng'],
'DaLian': ['JinZhou', 'JinShiTan', 'ZhuangHe', 'WaFangDian']
},
'JiLi': {'ChangChun': ['NanGuan', 'JiuTai', 'LvYuan'],
'SiPing': ['TieXi', 'TieDong', 'ShuangLiao', 'LiShuXian'],
'SongYuan': ['NingJiang', 'ChangLing', 'FuYu'],
},
'HeiLongJiang': {'HaErBing': ['TongHe', 'MuLan', 'YiLan', 'Hulan'],
'QiQiHaEr': ['KeShan', 'LongSha', 'JianHua', 'LongJiang'],
'DaQing': ['LongFeng', 'DaTong', 'HongGang', 'LinXun']
}
}
flag = True # 定义变量条件,改变条件程序结束
while flag: # 当flag=True时输出menu[key],第一层菜单
print(" Welcome to inquire city information")
print("---------------Province-------------------")
for key in city_dict:
print(key)
print("------------------------------------------")
province_input = input(">:").strip() # 定义变量键入province_input
if province_input == 'q': # 当输入“q” 时退出整个程序
flag = False
continue
if len(province_input) == 0: # 当输入变量为空时,结束本次循环重新开始本层循环
continue
if province_input in city_dict: # 判断输入变量province_input是否在city_dict中
while flag:
net_layer = city_dict[province_input] # 给予city_dict[province_input]定义本层菜单新变量net_layer
print("---------------City-------------------")
for key2 in net_layer: # 输出第二层菜单
print(key2)
print("--------------------------------------")
city_input = input(">>:").strip() # 定义变量输入city_input
if city_input == 'b': # 当输入变量city_input为“b“时,返回结束本层循环,返回上一层循环
break
if city_input == 'q': # 当输入“q” 时退出整个程序
flag = False
continue
if len(city_input) == 0: # 当输入变量为空时,结束本次循环重新开始本层循环
continue
if city_input in net_layer: # 判断输入变量是否在net_layer中
while flag:
net_layer2 = net_layer[city_input]
print("---------------District-------------------")
for key3 in net_layer2:
print(key3) # 输出第三层菜单
print("------------------------------------------")
district_inout = input(">>>:").strip()
if district_inout == 'b':
break
if district_inout == 'q':
flag = False
continue
if district_inout in net_layer2:
print("Sorry, no more details")
continue