python 省市区三级联动,back回到上一级,quit退出
dict_db = {
"河南": [
{
"郑州": [
"中原区",
"金水区",
"二七区",
"上街区",
"巩义市",
"新郑市",
"新密市",
"登封市",
"荥阳市",
"中牟县"
]
},
{
"开封": [
"鼓楼区",
"龙亭区",
"禹王台区",
"金明区",
"开封县",
"尉氏县",
"兰考县",
"杞县",
"通许县"
]
},
{
"南阳": [
"卧龙区",
"宛城区",
"邓州市",
"桐柏县",
"方城县",
"淅川县",
"镇平县",
"唐河县",
"南召县",
"内乡县"
]
},
{
"洛阳": [
"西工区",
"老城区",
"涧西区",
"瀍河回族区",
"洛龙区",
"吉利区",
"偃师市",
"孟津县",
"汝阳县"
]
}
],
"湖南": [
{
"长沙": [
"岳麓区",
"芙蓉区",
"天心区",
"开福区",
"雨花区",
"浏阳市",
"长沙县"
]
},
{
"岳阳": [
"岳阳楼区",
"云溪区",
"君山区",
"临湘市",
"汨罗市",
"岳阳县"
]
},
{
"株洲": [
"天元区",
"荷塘区",
"芦淞区",
"石峰区",
"醴陵市",
"株洲县",
"炎陵县"
]
},
{
"湘潭": [
"岳塘区",
"雨湖区",
"湘乡市",
"韶山市",
"湘潭县"
]
}
],
"广东": [
{
"广州": [
"越秀区",
"荔湾区",
"海珠区",
"天河区",
"白云区",
"黄埔区",
"番禺区",
"花都区"
]
},
{
"深圳": [
"福田区",
"罗湖区",
"南山区",
"宝安区",
"龙岗区",
"盐田区"
]
},
{
"珠海": [
"香洲区",
"斗门区",
"金湾区"
]
}
]
}
while True:
print("----------------------开始打印省--------------------------")
province_dict = {}
province_nos = []
for index, item in enumerate(dict_db.keys(), 1):
print("省份id:{}, 省名称:{}".format(index, item))
province_nos.append(index)
province_dict[index] = item
input_pro = input("请输入省份id(quit退出):")
if input_pro == "quit":
quit()
elif int(input_pro) not in province_nos:
print("输入省份id有误,请检查")
elif input_pro == "back":
break
else:
while True:
print("----------------------开始打印市--------------------------")
city_dict = {}
city_nos = []
for index, item in enumerate(dict_db[province_dict[int(input_pro)]], 1):
city_nos.append(index)
city_dict[index] = item
for key in item.keys():
print("市id:{}, 市名称:{}".format(index, key))
input_city = input("请输入省份id(quit退出back返回):")
if input_city == "quit":
quit()
elif input_city == "back":
break
elif int(input_city) not in city_nos:
print("输入市id有误,请检查")
else:
while True:
print("----------------------开始打印县级市--------------------------")
area_dict = {}
area_nos = []
for item_all in city_dict[int(input_city)].values():
for index, item in enumerate(item_all, 1):
area_nos.append(index)
area_dict[index] = item
print("区id:{}, 区名称:{}".format(index, item))
input_area = input("请输入(quit退出back返回):")
if input_area == "quit":
quit()
elif input_area.strip() == "back":
break
elif int(input_area) not in area_nos:
print("输入区id有误,请检查")
else:
exit()