日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

    歡迎訪問 生活随笔!

    生活随笔

    當(dāng)前位置: 首頁 >

    Python练习_三级菜单

    發(fā)布時(shí)間:2025/6/17 36 豆豆
    生活随笔 收集整理的這篇文章主要介紹了 Python练习_三级菜单 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

    打印省、市、縣三級(jí)菜單

    可返回上一級(jí)

    可隨時(shí)退出程序

    用遞歸實(shí)現(xiàn):menu = {'北京': {'海淀': {'五道口': {'soho': {},'網(wǎng)易': {},'google': {}},'中關(guān)村': {'愛奇藝': {},'汽車之家': {},'youku': {},},'上地': {'百度': {},},},'昌平': {'沙河': {'老男孩': {},'北航': {},},'天通苑': {},'回龍觀': {},},'朝陽': {},'東城': {},},'上海': {'閔行': {"人民廣場(chǎng)": {'炸雞店': {}}},'閘北': {'火車戰(zhàn)': {'攜程': {}}},'浦東': {},},'山東': {}, } 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

    -

    用堆棧實(shí)現(xiàn) menu = {'北京': {'海淀': {'五道口': {'soho': {},'網(wǎng)易': {},'google': {}},'中關(guān)村': {'愛奇藝': {},'汽車之家': {},'youku': {},},'上地': {'百度': {},},},'昌平': {'沙河': {'老男孩': {},'北航': {},},'天通苑': {},'回龍觀': {},},'朝陽': {},'東城': {},},'上海': {'閔行': {"人民廣場(chǎng)": {'炸雞店': {}}},'閘北': {'火車戰(zhàn)': {'攜程': {}}},'浦東': {},},'山東': {}, }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.設(shè)計(jì)一個(gè)三級(jí)菜單 2.在任意時(shí)候可以返回上級(jí)菜單 3.在任何一級(jí)菜單里面可以隨時(shí)退出程序思路:設(shè)計(jì)一個(gè)嵌套字典用于存儲(chǔ)菜單內(nèi)容--->利用嵌套循環(huán)打印每一級(jí)的內(nèi)容--->然后判斷輸入條件根據(jù)輸入條件選擇是否退出和返回上級(jí) ''' data = {"武漢":{"光谷":{"關(guān)山":['文華','華科','湖科']},"江夏":{"郊外":['湖經(jīng)','楚天','紡大']}},"北京":{'朝陽':{'a':[1,2,3]}},"上海":{"市內(nèi)":{'b':[2,3,4]}} } print(data) exit_flag = False#設(shè)置一個(gè)退出標(biāo)志 while not exit_flag:for i in data:print(i)#第一層choice = input('選擇進(jìn)入1>>>:')if choice in data:while not exit_flag:for i2 in data[choice]:print('\t', i2)#第二層choice2 = input('選擇進(jìn)入2>>>:')if choice2 in data[choice]:while not exit_flag:for i3 in data[choice][choice2]:print('\t\t', i3)#第三層choice3 = input('選擇進(jìn)入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ū)':{'中泰':{},'臨平':{}},'西湖區(qū)':{'西湖':{},'留下':{}}},'溫州市':{'蒼南縣':{'靈溪':{},'龍港':{}},'瑞安縣':{'安陽':{},'錦湖':{}}}},'廣東省':{'廣州市':{'越秀區(qū)':{'人民路':{},'北京路':{}},'荔灣區(qū)':{'沙面':{},'龍津':{}}},'珠海市':{'香洲區(qū)':{'拱北':{},'吉大':{}},'金灣區(qū)':{'紅旗鎮(zhèn)':{},'平沙鎮(zhèn)':{}}}}}# 返回標(biāo)記 q_flag = True # 退出標(biāo)記 Q_flag = True # 返回,退出標(biāo)記出現(xiàn)False退出循環(huán) while q_flag and Q_flag:# 遍歷打印省份for i in menu:print(i)province = input('請(qǐng)輸入要查找的省份(Q,退出):').strip()if province in menu:while q_flag and Q_flag:for i in menu[province]:print(i)city = input('請(qǐng)輸入要查找的市(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ǐng)輸入要查找的區(qū)或縣(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 退出該層循環(huán),返回上層循環(huán)q_flag = Falseelif choice == 'Q':# 退出大循環(huán)Q_flag = Falseelse:# 提示輸入不合法print('輸入錯(cuò)誤!')# 改回 q_flag 的值,實(shí)現(xiàn)只退出一層循環(huán)else:q_flag = Trueelif county == 'q':q_flag = Falseelif county == 'Q':Q_flag = Falseelse:print('輸入錯(cuò)誤!')else:q_flag = Trueelif city == 'q':q_flag = Falseelif city == 'Q':Q_flag = Falseelse:print('輸入錯(cuò)誤!')else:q_flag = Trueelif province == 'Q':Q_flag = Falseelse:print('輸入錯(cuò)誤!')

    -

    # _author : Ahern Li # @_date : 2017/9/12 menu = {'浙江省':{'杭州市':{'余杭區(qū)':{'中泰':{},'臨平':{}},'西湖區(qū)':{'西湖':{},'留下':{}}},'溫州市':{'蒼南縣':{'靈溪':{},'龍港':{}},'瑞安縣':{'安陽':{},'錦湖':{}}}},'廣東省':{'廣州市':{'越秀區(qū)':{'人民路':{},'北京路':{}},'荔灣區(qū)':{'沙面':{},'龍津':{}}},'珠海市':{'香洲區(qū)':{'拱北':{},'吉大':{}},'金灣區(qū)':{'紅旗鎮(zhèn)':{},'平沙鎮(zhèn)':{}}}}}current_layer = menu # 實(shí)現(xiàn)動(dòng)態(tài)循環(huán) parent_layer = [] # 保留所有父層,最后一個(gè)元素永遠(yuǎn)為父層while True:print() # 僅為了打印美觀for i in current_layer: # 遍歷打印地址print(i)choice = input('請(qǐng)?jiān)谙路捷斎氩樵兊刂穃n>>>:').strip()if choice in current_layer:if current_layer[choice]: # 判斷是否為末層parent_layer.append(current_layer) # 進(jìn)入子層前,添加當(dāng)前層作為父層current_layer = current_layer[choice] # 修改子層else:print('當(dāng)前是最后一頁')elif choice == '':continueelif choice == 'q': # 返回上層if parent_layer: # 判斷 parent_layer 是否為空current_layer = parent_layer.pop() # 取出當(dāng)前層父層# 退出循環(huán)elif choice == 'Q':breakelse:print('輸入錯(cuò)誤!')

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/dongye95/p/10193479.html

    總結(jié)

    以上是生活随笔為你收集整理的Python练习_三级菜单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。