摘录自:雨神,供参考!
province_dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
"唐山": ["唐山1", "唐山2", "唐山3", "唐山4", "唐山5"]
},
"河南": {
"郑州": ["新郑", "新密", "金水区"],
"安阳": ["安阳县", "滑县", "汤阴县", "滑县", "张家庄"],
"许昌": ["魏都区","长葛","禹州"],
"焦作": ["焦作", "焦作", "焦作","焦作","焦作","焦作"],
"新乡": ["新乡1", "新乡2", "新乡3", "新乡4", "新乡5"],
"洛阳": ["洛阳1","洛阳2","洛阳3","洛阳4","洛阳5","洛阳6"]
},
"山西": {
"太原": ["太原1", "太原2", "太原3", "太原4", "太原5"],
"运城": ["运城1", "运城2", "运城3"],
"阳泉": ["阳泉1", "阳泉2", "阳泉3", "阳泉4"],
"大同": ["大同1", "大同2", "大同3", "大同4", "大同5", "大同6"]
},
"湖南":{
"长沙": ["长沙1","长沙2","长沙3"],
"湘潭": ["湘潭1","湘潭2","湘潭3"],
"岳阳": ["岳阳1","岳阳2","岳阳3","岳阳4"],
"常宁": ["常宁1","常宁2","常宁3","常宁4","常宁5","常宁6"]
}
}
province_list = []
city_list = []
county_list = []
province_tag = True
while province_tag:
province_list.clear()
for i in province_dic:
province_list.append(i)
num = 0
print( '请输入您想查询的省份:')
while(num<(len(province_list)+1)):
if(num<len(province_list)):
print(' %d-->%s' % (num+1,province_list[num]))
else:
print(' %d-->%s' % (num+1,'退出'))
num+=1
province_num = input()
if(province_num.isdigit() and int(province_num) in range(1,num+1)):
if int(province_num) == num: #退出 选择激活!此时num=5,等于退出
province_tag = False
break
m_province = province_list[int(province_num)-1]
print('您选择的省份为%s' % m_province)
city_tag = True
city_list.clear()
for i in province_dic.get(m_province):
city_list.append(i)
else:
print('请按照提示输入,谢谢')
continue
while city_tag:
num=0
print( '请输入您想查询的市:')
while(num<(len(city_list)+2)):
if(num<len(city_list)):
print(' %d-->%s' % (num+1,city_list[num]))
elif(num==len(city_list)):
print(' %d-->%s' % (num+1,'返回上一级'))
else:
print(' %d-->%s' % (num+1,'退出'))
num+=1
city_num = input()
if(city_num.isdigit() and int(city_num) in range(1,num+1)):
if(int(city_num)==num-1):
break
if(int(city_num)==num):
province_tag=False
break
m_city = city_list[int(city_num)-1]
print('您选择的市为%s' % m_city)
county_list.clear()
for i in (province_dic.get(m_province)).get(m_city):
county_list.append(i)
else:
print('请按照提示输入,谢谢')
continue
while city_tag:
num=0
print( '请输入您想查询的县(区):')
while(num<(len(county_list)+2)):
if(num<len(county_list)):
print(' %d-->%s' % (num+1,county_list[num]))
elif(num==len(county_list)):
print(' %d-->%s' % (num+1,'返回上一级'))
else:
print(' %d-->%s' % (num+1,'退出'))
num+=1
county_num = input()
if(county_num.isdigit() and int(county_num) in range(1,num+1)):
if(int(county_num)==num-1):
break
if(int(county_num)==num):
city_tag=False
province_tag=False
break
m_county = county_list[int(county_num)-1]
print('您选择的县(区)为%s' % m_county)
temp_num = input('''
1-->重新选择
2-->返回上级
3-->重新开始
4-->退出
''')
if(temp_num.isdigit() and int(temp_num) in range(1,5)):
if(temp_num==''):
continue
if(temp_num==''):
break
if(temp_num==''):
city_tag=False
break
if(temp_num==''):
city_tag=False
province_tag=False
break
else:
print('请按照提示输入,谢谢')
continue
else:
print('请按照提示输入,谢谢')
continue
雨神之省市县三级联动
# tag = True
# while tag:
# for shengming in province_dic:
# print(shengming)
# shengming = input("哪个省:(q退出!)")
# if shengming.strip() in province_dic:
# while tag:
# for chengshiming in province_dic[shengming.strip()]:
# print(chengshiming)
# chengshiming = input("哪个城市:(q退出!b返回)")
# if chengshiming.strip() in province_dic[shengming.strip()]:
# while tag:
# for quming in province_dic[shengming.strip()][chengshiming.strip()]:
# print(quming)
# quming = input("q退出!b返回!")
# if quming.strip() == 'q':
# tag = False
# elif quming.strip() == "b":
# break
# elif chengshiming.strip() == "q":
# tag = False
# elif chengshiming.strip() == 'b':
# break
# elif shengming.strip() == "q":
# tag = False
# print("你已退出本系统,假期愉快!") # current_layers = province_dic
# layers = [province_dic]
#
# while True:
# for i in current_layers:
# print(i)
# choice = input(">>: ").strip()
# if choice in current_layers:
# layers.append(current_layers)
# current_layers = current_layers[choice]
# elif choice.upper() == "B":
# current_layers = layers[-1]
# layers.pop()
学无止境