Python練習_三級菜單
阿新 • • 發佈:2018-12-29
ice date code urn 廣州 遍歷 西湖 實現 user
打印省、市、縣三級菜單
可返回上一級
可隨時退出程序
用遞歸實現: menu = { ‘北京‘: { ‘海澱‘: { ‘五道口‘: { ‘soho‘: {}, ‘網易‘: {}, ‘google‘: {} }, ‘中關村‘: { ‘愛奇藝‘: {}, ‘汽車之家‘: {},‘youku‘: {}, }, ‘上地‘: { ‘百度‘: {}, }, }, ‘昌平‘: { ‘沙河‘: { ‘老男孩‘: {}, ‘北航‘: {}, }, ‘天通苑‘: {}, ‘回龍觀‘: {}, }, ‘朝陽‘: {},‘東城‘: {}, }, ‘上海‘: { ‘閔行‘: { "人民廣場": { ‘炸雞店‘: {} } }, ‘閘北‘: { ‘火車戰‘: { ‘攜程‘: {} } }, ‘浦東‘: {}, }, ‘山東‘: {}, } def threeMenu(dic): while 1:for k in dic: print(k) key = input(‘輸入城市(按b返回上一層,按q退出):‘) if key == ‘b‘ or key == ‘q‘: return key elif key in dic.keys() and dic[key]: ret = threeMenu(dic[key]) if ret == ‘q‘: return ret elif (not dic.get(key)) or (not dic[key]): continue
-
用堆棧實現 menu = { ‘北京‘: { ‘海澱‘: { ‘五道口‘: { ‘soho‘: {}, ‘網易‘: {}, ‘google‘: {} }, ‘中關村‘: { ‘愛奇藝‘: {}, ‘汽車之家‘: {}, ‘youku‘: {}, }, ‘上地‘: { ‘百度‘: {}, }, }, ‘昌平‘: { ‘沙河‘: { ‘老男孩‘: {}, ‘北航‘: {}, }, ‘天通苑‘: {}, ‘回龍觀‘: {}, }, ‘朝陽‘: {}, ‘東城‘: {}, }, ‘上海‘: { ‘閔行‘: { "人民廣場": { ‘炸雞店‘: {} } }, ‘閘北‘: { ‘火車戰‘: { ‘攜程‘: {} } }, ‘浦東‘: {}, }, ‘山東‘: {}, } l = [menu] while l: for k in l[-1]: print(k) key = input(‘輸入城市(按b返回上一層,按q退出):‘) if key in l[-1].keys() and l[-1][key]: l.append(l[-1][key]) elif key == ‘b‘: l.pop() elif key == ‘q‘: break else: continue
#!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi ‘‘‘需求: 1.設計一個三級菜單 2.在任意時候可以返回上級菜單 3.在任何一級菜單裏面可以隨時退出程序 思路:設計一個嵌套字典用於存儲菜單內容 --->利用嵌套循環打印每一級的內容 --->然後判斷輸入條件根據輸入條件選擇是否退出和返回上級 ‘‘‘ data = { "武漢": { "光谷":{ "關山":[‘文華‘,‘華科‘,‘湖科‘] }, "江夏":{ "郊外":[‘湖經‘,‘楚天‘,‘紡大‘] } }, "北京":{ ‘朝陽‘:{ ‘a‘:[1,2,3] } }, "上海":{ "市內":{ ‘b‘:[2,3,4] } } } print(data) exit_flag = False#設置一個退出標誌 while not exit_flag: for i in data: print(i)#第一層 choice = input(‘選擇進入1>>>:‘) if choice in data: while not exit_flag: for i2 in data[choice]: print(‘\t‘, i2)#第二層 choice2 = input(‘選擇進入2>>>:‘) if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print(‘\t\t‘, i3)#第三層 choice3 = input(‘選擇進入3>>>:‘) if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print(‘\t\t‘, i4) choice4 = input(‘最後一層,按b返回>>>:‘) if choice4 == ‘b‘: pass elif choice4 == ‘q‘: exit_flag = True if choice3 == ‘b‘: break elif choice3 == ‘q‘: exit_flag = True if choice2 == ‘b‘: break elif choice2 == ‘q‘: exit_flag = True elif choice == ‘q‘: exit_flag = True
-
# _author : Ahern Li # @_date : 2017/9/12 menu = { ‘浙江省‘:{ ‘杭州市‘:{ ‘余杭區‘:{‘中泰‘:{},‘臨平‘:{}}, ‘西湖區‘:{‘西湖‘:{},‘留下‘:{}} }, ‘溫州市‘:{ ‘蒼南縣‘:{‘靈溪‘:{},‘龍港‘:{}}, ‘瑞安縣‘:{‘安陽‘:{},‘錦湖‘:{}} } }, ‘廣東省‘:{ ‘廣州市‘:{ ‘越秀區‘:{‘人民路‘:{},‘北京路‘:{}}, ‘荔灣區‘:{‘沙面‘:{},‘龍津‘:{}} }, ‘珠海市‘:{ ‘香洲區‘:{‘拱北‘:{},‘吉大‘:{}}, ‘金灣區‘:{‘紅旗鎮‘:{},‘平沙鎮‘:{}} } } } # 返回標記 q_flag = True # 退出標記 Q_flag = True # 返回,退出標記出現False退出循環 while q_flag and Q_flag: # 遍歷打印省份 for i in menu: print(i) province = input(‘請輸入要查找的省份(Q,退出):‘).strip() if province in menu: while q_flag and Q_flag: for i in menu[province]: print(i) city = input(‘請輸入要查找的市(q,返回 或 Q,退出):‘).strip() if city in menu[province]: while q_flag and Q_flag: for i in menu[province][city]: print(i) county = input(‘請輸入要查找的區或縣(q,返回 或 Q,退出):‘).strip() if county in menu[province][city]: while q_flag and Q_flag: for i in menu[province][city][county]: print(i) # 提示最後一頁,輸入格式 choice = input(‘最後一頁! q,返回 或 Q,退出:‘).strip() if choice == ‘q‘: # 配合 else: q_flag = True 退出該層循環,返回上層循環 q_flag = False elif choice == ‘Q‘: # 退出大循環 Q_flag = False else: # 提示輸入不合法 print(‘輸入錯誤!‘) # 改回 q_flag 的值,實現只退出一層循環 else: q_flag = True elif county == ‘q‘: q_flag = False elif county == ‘Q‘: Q_flag = False else: print(‘輸入錯誤!‘) else: q_flag = True elif city == ‘q‘: q_flag = False elif city == ‘Q‘: Q_flag = False else: print(‘輸入錯誤!‘) else: q_flag = True elif province == ‘Q‘: Q_flag = False else: print(‘輸入錯誤!‘)
-
# _author : Ahern Li # @_date : 2017/9/12 menu = { ‘浙江省‘:{ ‘杭州市‘:{ ‘余杭區‘:{‘中泰‘:{},‘臨平‘:{}}, ‘西湖區‘:{‘西湖‘:{},‘留下‘:{}} }, ‘溫州市‘:{ ‘蒼南縣‘:{‘靈溪‘:{},‘龍港‘:{}}, ‘瑞安縣‘:{‘安陽‘:{},‘錦湖‘:{}} } }, ‘廣東省‘:{ ‘廣州市‘:{ ‘越秀區‘:{‘人民路‘:{},‘北京路‘:{}}, ‘荔灣區‘:{‘沙面‘:{},‘龍津‘:{}} }, ‘珠海市‘:{ ‘香洲區‘:{‘拱北‘:{},‘吉大‘:{}}, ‘金灣區‘:{‘紅旗鎮‘:{},‘平沙鎮‘:{}} } } } current_layer = menu # 實現動態循環 parent_layer = [] # 保留所有父層,最後一個元素永遠為父層 while True: print() # 僅為了打印美觀 for i in current_layer: # 遍歷打印地址 print(i) choice = input(‘請在下方輸入查詢地址\n>>>:‘).strip() if choice in current_layer: if current_layer[choice]: # 判斷是否為末層 parent_layer.append(current_layer) # 進入子層前,添加當前層作為父層 current_layer = current_layer[choice] # 修改子層 else: print(‘當前是最後一頁‘) elif choice == ‘‘: continue elif choice == ‘q‘: # 返回上層 if parent_layer: # 判斷 parent_layer 是否為空 current_layer = parent_layer.pop() # 取出當前層父層 # 退出循環 elif choice == ‘Q‘: break else: print(‘輸入錯誤!‘)
Python練習_三級菜單