Python练习_三级菜单
Posted dongye95
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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(‘ ‘, i2)#第二层 choice2 = input(‘选择进入2>>>:‘) if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print(‘ ‘, i3)#第三层 choice3 = input(‘选择进入3>>>:‘) if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print(‘ ‘, 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(‘请在下方输入查询地址 >>>:‘).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练习_三级菜单的主要内容,如果未能解决你的问题,请参考以下文章