字典三級選單
阿新 • • 發佈:2018-11-21
第一種 多層while 迴圈方法
1 menu = { 2 '北京':{ 3 '海淀':{ 4 '五道口':{ 5 'soho':{}, 6 '網易':{}, 7 'google':{} 8 }, 9 '中關村':{ 10 '愛奇藝':{}, 11 '汽車之家':{}, 12 'youku':{}, 13 }, 14 '上地':{ 15 '百度':{}, 16 }, 17 }, 18 '昌平':{ 19 '沙河':{ 20 '老男孩':{}, 21 '北航':{}, 22 }, 23 '天通苑':{}, 24 '回龍觀':{}, 25 }, 26 '朝陽':{}, 27 '東城':{}, 28 }, 29 '上海':{ 30 '閔行':{ 31 "人民廣場":{ 32 '炸雞店':{} 33 } 34 }, 35 '閘北':{ 36 '火車站':{ 37 '攜程':{} 38 } 39 }, 40 '浦東':{}, 41 }, 42 '山東':{}, 43 }
1 brk_flag = True #標記 2 while brk_flag: 3 for i in menu: 4 print(i) 5 ch = input('1請輸入城市名字,或者按q退出').strip() 6 if ch != 'q' and ch != 'b': 7 if menu.get(ch) and menu[ch].values(): #判斷輸入的這個城市是否存在和判斷是不是最後一層 8 for i in menu[ch]: #迴圈遍歷,列印 9 print(i) 10 while brk_flag: # 第二層迴圈 11 ch2 = input('2請輸入城市名稱,按b返回上層,或者q退出').strip() 12 if ch2 != 'q' and ch2 != 'b': 13 if menu[ch].get(ch2) and menu[ch][ch2].values(): 14 for i in menu[ch][ch2]: 15 print(i) 16 while brk_flag: #第三層迴圈 17 ch3 = input('3請輸入城市名稱,按b返回上層,或者q退出').strip() 18 if ch3 != 'q' and ch3 != 'b': 19 if menu[ch][ch2].get(ch3) and menu[ch][ch2][ch3].values(): 20 for i in menu[ch][ch2][ch3]: 21 print(i) 22 while brk_flag:#第四層迴圈 23 ch4 = input('4請輸入城市名稱,按b返回上層,或者q退出').strip() 24 if ch4 != 'q' and ch4 != 'b': 25 if menu[ch][ch2][ch3].get(ch4) and menu[ch][ch2][ch3][ch4].values(): 26 for i in menu[ch][ch2][ch3][ch4]: 27 print(i) 28 elif menu[ch][ch2][ch3].get(ch4) == None: 29 print('輸入錯誤,重新輸入') 30 for i in menu[ch][ch2][ch3]: 31 print(i) 32 continue 33 else: 34 print(f'該{ch4}目錄是底部,請重新輸入') 35 for i in menu[ch][ch2][ch3]: 36 print(i) 37 continue 38 elif ch4 == 'q': 39 brk_flag = True 40 else: 41 for i in menu[ch][ch2]: 42 print(i) 43 break 44 45 elif menu[ch][ch2].get(ch3) == None: 46 print('輸入錯誤,重新輸入') 47 for i in menu[ch][ch2]: 48 print(i) 49 continue 50 else: 51 print(f'該{ch3}目錄是底部,請重新輸入') 52 for i in menu[ch][ch2]: 53 print(i) 54 continue 55 elif ch3 == 'q': 56 brk_flag = False 57 else: 58 for i in menu[ch]: 59 print(i) 60 break 61 elif menu[ch].get(ch2) == None : 62 print('輸入錯誤,重新輸入') 63 for i in menu[ch]: 64 print(i) 65 continue 66 else: 67 print(f'該{ch2}目錄是底部,請重新輸入') 68 for i in menu[ch]: 69 print(i) 70 continue 71 elif ch2 == 'q': 72 brk_flag = False 73 else: 74 break 75 elif menu.get(ch) == None: #輸入的不存在,繼續迴圈要求輸入 76 print('輸入錯誤,重新輸入') 77 continue 78 else: 79 print(f'該{ch}目錄到底了,請重新輸入') 80 continue 81 elif ch == 'b': 82 print('該城是頂層,不支援返回,請重新輸入') 83 continue 84 else: 85 breakView Code
第二種 函式巢狀
def con():# 進入字典 for i in menu: print(i) ch = input('1請輸入城市名稱,或者按q退出:').strip() if ch != 'q' and ch != 'b': if menu.get(ch) and menu[ch].values(): #判斷該值是否存在以及該值目錄下面還有沒有 def con1(): #通過函式巢狀再次進入迴圈。(第一層) for i in menu[ch]: print(i) ch2 = input('2請輸入名稱,按b返回上層目錄,或者按q退出:').strip() if ch2 != 'q' and ch2 != 'b': if menu[ch].get(ch2) and menu[ch][ch2].values(): def con2():#(第二層) for i in menu[ch][ch2]: print(i) ch3 = input('3請輸入名稱,按b返回上層目錄,或者按q退出:').strip() if ch3 != 'q' and ch3 != 'b': if menu[ch][ch2].get(ch3) and menu[ch][ch2][ch3].values(): def con3():#(第三層) for i in menu[ch][ch2][ch3]: print(i) ch4 = input('4請輸入名稱,按b返回上層目錄,或者按q退出:') if ch4 != 'q' and ch4 != 'b': if menu[ch][ch2][ch3].get(ch4) and menu[ch][ch2][ch3][ch4].values(): def con4():#(第四層) for i in menu[ch][ch2][ch3][ch4]: print(i) con4() elif menu[ch][ch2][ch3].get(ch4) == None: print('輸入錯誤,重新輸入') con3() else: print(f'該{ch4}目錄到底了,請重新輸入') con3() elif ch4 == 'b': con2() else: return con3() elif menu[ch][ch2].get(ch3) == None: print('輸入有誤,請重新輸入') con2() else: print(f'該{ch3}目錄到底了,請重新輸入') con2() elif ch3 == 'b': con1() else: return con2() elif menu[ch].get(ch2) == None: print('輸入有誤,請重新輸入') con1() else: print(f'該{ch2}目錄到底了,請重新輸入') con1() elif ch2 == 'b': con() else: return con1() elif menu.get(ch) == None: #如果該值不存在,提示重新輸入,呼叫該層函式 print('輸入有誤,請重新輸入') con() else: #如果判斷該目錄下面為空,則提示重新輸入 print(f'該{ch}目錄到底了,請重新輸入') con() elif ch == 'b': print('目錄到頂層了') con() else: return while True: con() breakView Code
第三種,列表迴圈
1 current_layer = menu 2 # 將字典存入列表中 3 layer = [] 4 while True: 5 for i in current_layer: 6 print(i) 7 choice = input('請輸入城市').strip() 8 if not choice: 9 continue 10 if choice in current_layer: 11 if current_layer[choice].values(): 12 layer.append(current_layer) 13 current_layer = current_layer[choice] 14 else: 15 print('目錄到底了,重新輸入') 16 time.sleep(0.5) 17 elif choice == 'q': 18 break 19 elif choice == 'b': 20 if not layer: 21 print('頂部,重新輸入') 22 time.sleep(0.5) 23 else: 24 current_layer = layer.pop(-1) 25 else: 26 print('輸入錯誤,重新輸入') 27 time.sleep(0.5)View Code