Python 练习:三级菜单选择城市

时间:2023-01-22 14:48:51
info = {
'GuangDong':{
'GuangZhou': ['TianHe', 'HaiZhu'],
'MaoMing': ['MaoNan', 'DianBai']},
'ShanDong': {
'JiNan': ['ShiZhong', 'LiXia'],
'QingDao': ['ShiNan', 'ShiBei']
},
'GuangXi': {
'NanNin': ['WuNing', 'LongAn'],
'GuiLing': ['YangSuo', 'QuanZhou']
}
} flag = True while flag:
print(''.center(50, '#'))
for p in info.keys():
print("省份:", p)
province = input("Please input the province: [e : exit]")
if province == 'e':
break
if (province in list(info.keys())) == False:
print("You need to input right province!")
continue
while True:
print(''.center(50, '#'))
for c in info[province].keys():
print("城市: ", c)
city = input("Please input the city: [q: return; e: exit]")
if city == 'q':
break
if city == 'e':
flag = False
break
if (city in list(info[province].keys())) == False:
print("You need to input right city!")
continue
for k in info[province][city]:
print("区县: ", k)

运行结果:

Python 练习:三级菜单选择城市