Python练习_三级菜单
生活随笔
收集整理的這篇文章主要介紹了
Python练习_三级菜单
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
打印省、市、縣三級菜單
可返回上一級
可隨時退出程序
用遞歸實現: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 keyelif key in dic.keys() and dic[key]:ret = threeMenu(dic[key])if ret == 'q': return retelif (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':breakelse: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':passelif choice4 == 'q':exit_flag = Trueif choice3 == 'b':breakelif choice3 == 'q':exit_flag = Trueif choice2 == 'b':breakelif choice2 == 'q':exit_flag = Trueelif 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 = Falseelif choice == 'Q':# 退出大循環Q_flag = Falseelse:# 提示輸入不合法print('輸入錯誤!')# 改回 q_flag 的值,實現只退出一層循環else:q_flag = Trueelif county == 'q':q_flag = Falseelif county == 'Q':Q_flag = Falseelse:print('輸入錯誤!')else:q_flag = Trueelif city == 'q':q_flag = Falseelif city == 'Q':Q_flag = Falseelse:print('輸入錯誤!')else:q_flag = Trueelif province == 'Q':Q_flag = Falseelse: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 == '':continueelif choice == 'q': # 返回上層if parent_layer: # 判斷 parent_layer 是否為空current_layer = parent_layer.pop() # 取出當前層父層# 退出循環elif choice == 'Q':breakelse:print('輸入錯誤!')?
轉載于:https://www.cnblogs.com/dongye95/p/10193479.html
總結
以上是生活随笔為你收集整理的Python练习_三级菜单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 变量及变量操作步骤
- 下一篇: 数据库各个派系的起源和应用场景