Python编写地区选择-Dict
#!-*- coding:utf-8 -*-
"""
Mut_select of the place name.
1.1-3 to Three layer
2."q" to exit.
3."b" to back.
4.other Words to retry.
5.add the values in the dict,no infact
需要改进:
在交互界面增加和删除词典里的值
"""
PlaceName = {"辽宁": {"大连": ("高新", "中山"), "沈阳": ("铁西", "浑南"), "葫芦岛": ("莲山", "兴城")},
"吉林": {"长春": ("大洼", "高天"), "四平": ("二道河", "路阳"), "通辽": ("通化", "黑道")},
"黑龙江": {"哈尔": ("坪山", "故阳"), "大庆": ("黑油", "千多"), "漠河": ("俄国", "最北"), "毅力": ("坚强", "努力")}}
def province(place_name): # 定义选择菜单
province_list = []
j = 1
for i in place_name.keys(): # 读取词典里的 键名
province_list = province_list+[i]
print(j, '.', i) # 在屏幕上打印选择菜单
j += 1
return province_list # 返回选择菜单的list
def judge_input(input_list): # 判断输入
pro_name = ""
pr_value = input("Please select number,press 'q' to exit:") # 获取输入的选项值
if ('1' <= pr_value <= str(len(input_list))) and len(pr_value) == 1: # 判断选择城市
pro_name = input_list[int(pr_value)-1] # 动态赋值,词典加减项,对本程序无影响
print("您选择%s,以下是它的1下属区域:" % pro_name) # 打印
return pro_name # 返回选择省/市的名字
elif pr_value == 'q' or pr_value == 'Q': # 判断退出,返回值0,供主程序判断
print("You quit the program!")
return '0'
elif pr_value == 'b' or pr_value == 'B': # 判断返回,返回值1,供主程序判断
return '-1'
else:
return
while True: # 无限循环,直到输入q/Q
ProvinceLists = province(PlaceName) # 调用读取词典键名函数,返回省份list
return_value_p = judge_input(ProvinceLists) # 调用输入判断函数,返回输入省值
if return_value_p == '0': # 0对应q/Q ,结束程序,第一级菜单b/B无意义,无须处理
break
elif return_value_p in PlaceName: # 判断输入键值存在字典里
CityName = PlaceName[return_value_p] # 读取选择省份值,存入
while True: # 进入选择市级菜单
CityLists = province(CityName) # 调用读取词典键名函数,返回城市list
return_value_c = judge_input(CityLists) # 调用输入判断函数,返回输入城市值
if return_value_c == '0': # 0对应q/Q,赋值,满足外层结束条件,结束程序
return_value_p = '0'
break
elif return_value_c in CityName: # 判断输入键值存在子字典里
CountryList = CityName[return_value_c] # 县级元组,存入
for name in CountryList: # 遍历元组值,输出
print(name)
while True: # 最下级菜单的输入判断
last_value = input("Please press 'b' to return or 'q' to quit:")
if last_value == 'q' or last_value == 'Q': # q/Q设置上两级循环的退出条件,退出程序
return_value_c = '0'
return_value_p = '0'
break
elif last_value == 'b' or last_value == 'B': # b/B退出循环
break
else:
continue # 输入其它选项,结束本次循环
elif return_value_c == '-1': # 城市层级,读取b/B,结束城市循环,返回省级
break
if return_value_p == '0': # 读取退出程序的条件,退出
break
if return_value_p == '0': # 读取退出程序的条件,退出
break
print('本次查询已经结束!')
#print(City["辽宁"]["沈阳"])
相关文章