python循环语句及字典综合练习——打印多级菜单
1 # -*- coding: utf-8 -*- 2 ''' 3 ----------------------------------------------- 4 多级菜单跳出循环失败:已解决 5 查询级别输出错误:已解决 6 ----------------------------------------------- 7 ''' 8 menuThirdlevel = { 9 "江苏省": { 10 "连云港市": { 11 "东海县": 12 ["白塔埠镇"], 13 "新浦区": 14 ["None!"]}, 15 "南京市": { 16 "雨花台区": 17 ["xxx.ccc"], 18 "玄武区": 19 [ "I don't know!"]}, 20 }, 21 "山西省": { 22 "太原市": { 23 "万柏林区": 24 ['瓦流路'], 25 "迎泽区": 26 ['a', 'b', 'c']}, 27 "aa市": { 28 "www": 29 ['1', '2', '3']}, 30 }, 31 '北京市': { 32 '海淀': { 33 '五道口': { 34 'soho':{}, 35 '网易':{}, 36 'google':{} 37 }, 38 '中关村':{ 39 '爱奇艺':{}, 40 '汽车之家':{}, 41 'youku':{}, 42 }, 43 '上地':{ 44 '百度':{}, 45 }, 46 }, 47 '昌平':{ 48 '沙河':{ 49 'ppp':{}, 50 '北航':{}, 51 }, 52 '天通苑':{}, 53 '回龙观':{}, 54 }, 55 '朝阳':{}, 56 '东城':{}, 57 }, 58 '上海市':{ 59 '闵行':{ 60 "人民广场":{ 61 '炸鸡店':{} 62 } 63 }, 64 '闸北':{ 65 '火车战':{ 66 '携程':{} 67 } 68 }, 69 '浦东':{}, 70 }, 71 '山东省':{}, 72 } 73 exit_flag = False 74 msg = "Thank you for your searching!" 75 print(list(menuThirdlevel.keys())) 76 def menu_f(): 77 provinceSearchTimes = 1 78 while provinceSearchTimes > 0: 79 provinceSearch = input("请输入您要查询的省级行政单位名称:\n") 80 if provinceSearch in list(menuThirdlevel.keys()): 81 print(list(menuThirdlevel[provinceSearch].keys())) 82 citySearchTimes = 1 83 while citySearchTimes > 0: 84 citySearch = input("请输入您要查询的市级行政单位名称:\n") 85 if citySearch in list(menuThirdlevel[str(provinceSearch)].keys()): 86 print(list(menuThirdlevel[str(provinceSearch)][str(citySearch)].keys())) 87 areaSearchTimes = 1 88 while areaSearchTimes > 0: 89 areaSearch = input("请输入您要查询的区县级行政单位(或其它)名称:\n") 90 if areaSearch in list(menuThirdlevel[str(provinceSearch)][citySearch].keys()): 91 print(list(menuThirdlevel[str(provinceSearch)][str(citySearch)][str(areaSearch)])) 92 print("search over!") 93 end_put_over = input("continue? [Y] END?[Any Key...]") 94 if end_put_over == "Y": 95 inputContinueTimes = 1 96 while inputContinueTimes < 4: 97 inputContinue = input("请输入%s下级行政单位名称编号[C:查询市级 A:查询区县级]:" % str(provinceSearch)) 98 if inputContinue == "C": 99 exit_flag = True 100 break 101 elif inputContinue == "A": 102 break 103 else: 104 print("invalid input!") 105 inputContinueTimes += 1 106 else: 107 print("search over!") 108 return msg 109 if exit_flag == True: 110 break 111 else: 112 print("search over!") 113 return msg 114 else: 115 print(bool(0)) 116 end_put_area = input("continue?[Y] end?[Any key...]") 117 if end_put_area == "Y": 118 print(list(menuThirdlevel[str(provinceSearch)][str(citySearch)].keys())) 119 areaSearchTimes += 1 120 else: 121 print("search over!") 122 return msg 123 else: 124 print(bool(0)) 125 end_put_city = input("continue?[Y] end?[Any key...]") 126 if end_put_city == "Y": 127 print(list(menuThirdlevel[provinceSearch].keys())) 128 citySearchTimes += 1 129 else: 130 print("search over!") 131 return msg 132 if exit_flag == True: 133 break 134 else: 135 print(bool(0)) 136 end_put_province = input("continue?[Y] end?[Any key...]") 137 if end_put_province == "Y": 138 print(list(menuThirdlevel.keys())) 139 provinceSearchTimes += 1 140 else: 141 print("search over!") 142 return msg 143 144 145 print(menu_f())
不乱于心,不困于情,不畏将来,不念过往,如此安好!