标签:mat lis users .json step tin break format day
1 ‘‘‘ 2 实现行政区划三级菜单 3 1. 每级菜单进入后显示所有选项 4 2. 输入某个选项进入下一级菜单 5 输错提示输错,重新输入 6 输入Q退出程序 7 输入B返回上一层 8 ‘‘‘ 9 10 import json 11 12 file_path = ‘/Users/Rex/PycharmProjects/pyexe/Day1/admin_divisions.json‘ 13 admin_div = {} 14 prov_list = [] 15 with open (file_path) as f_obj: 16 admin_div = json.load(f_obj) 17 18 num_pro = len(admin_div) #省个数 19 prov_list = list(admin_div.keys()) #省列表 20 21 while 1: 22 # 输出省列表 23 print("**********行政区划***********") 24 for i in range(num_pro): 25 print(prov_list[i]) 26 prov = input("输入你想查看的省(退出程序请输入Q+Enter):") 27 if(prov == "Q"): 28 exit(0) 29 else: 30 try: 31 city_list = list(admin_div[prov]) 32 except KeyError: 33 print("你输入的省不存在.") 34 continue 35 else: 36 while 1: 37 #输出市列表 38 print(‘**********行政区划-{p}***********‘.format(p = prov)) 39 num_city = len(city_list) 40 for i in range(num_city): 41 print(city_list[i]) 42 city = input("输入你想查看的市(退出程序请输入Q+Enter.返回上一层请输入B+Enter.):") 43 if (city == "Q"): 44 exit(0) 45 elif(city == ‘B‘): 46 break 47 else: 48 try: 49 county_list = admin_div[prov][city] 50 except KeyError: 51 print("你输入的市不存在.") 52 continue 53 else: 54 #输出县列表 55 print(‘**********行政区划-{c}***********‘.format(c=city)) 56 num_county = len(county_list) 57 for i in range(num_county): 58 print(county_list[i]) 59 while 1: 60 nextstep = input("退出请输入Q+Enter.返回上一层请输入B+Enter.") 61 if (nextstep == ‘B‘): 62 break 63 elif (nextstep == ‘Q‘): 64 exit(0) 65 else: 66 continue
标签:mat lis users .json step tin break format day
原文地址:http://www.cnblogs.com/jupiter1961/p/7739641.html