三级菜单python编码及高级编码
2024-09-08 19:41:53
# -*- coding: utf-8 -*-
# @Time : 2020/7/31 0:13
# @Author : Breeze
# @FileName: 三级菜单.py menu = {
'北京':{
'朝阳':{
'国贸':{
'CICC':{},
'HP':{},
'渣打银行':{}
},
'望京':{
'陌陌':{},
'奔驰':{},
'360':{},
},
'三里屯':{
'优衣库':{},
'apple':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'阿泰包子':{}
},
'天通苑':{
'链家':{},
'我爱我家':{},
},
'回龙观':{},
},
'海淀':{
'五道口':{
'谷歌':{},
'网易':{},
'快手':{},
'SOHU':{},
'SOGO':{},
},
'中关村':{
'YOUKU':{},
'IQIYI':{},
'汽车之家':{},
'新东方':{},
'QQ':{},
},
},
},
'上海':{
'浦东':{
'陆家嘴':{
'CICC':{},
'高盛':{},
'摩根':{},
},
'外滩':{},
},
'闵行':{},
'静安':{}, },
'山东':{
'济南':{},
'德州':{
'乐陵':{
'丁务镇':{},
'城区':{},
},
'平原':{},
},
'青岛':{},
}
} back_flag = False
exit_flag = False
while not back_flag and not exit_flag:
for key in menu:
print(key)
choice = input('>>>:').strip()
if choice in menu:
while not back_flag and not exit_flag:
for key2 in menu[choice]:
print(key2)
choice2 = input('>>>:').strip()
if choice2 == 'b':
back_flag = True
if choice2 == 'q':
exit_flag = True
if choice2 in menu[choice]:
while not back_flag and not exit_flag:
for key3 in menu[choice][choice2]:
print(key3)
choice3 = input('>>>:').strip()
if choice3 == 'b':
back_flag = True
if choice3 == 'q':
exit_flag = True
if choice3 in menu[choice][choice2]:
while not back_flag and not exit_flag:
for key4 in menu[choice][choice2][choice3]:
print(key4)
choice4 = input('>>>:').strip()
print('last level')
if choice4 == 'b':
back_flag = True
if choice4 == 'q':
exit_flag = True
else:
back_flag = False
else:
back_flag = False
else:
back_flag = False
# -*- coding: utf-8 -*-
# @Time : 2020/7/31 1:29
# @Author : Breeze
# @FileName: 三级菜单high.py menu = {
'北京':{
'朝阳':{
'国贸':{
'CICC':{},
'HP':{},
'渣打银行':{}
},
'望京':{
'陌陌':{},
'奔驰':{},
'360':{},
},
'三里屯':{
'优衣库':{},
'apple':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'阿泰包子':{}
},
'天通苑':{
'链家':{},
'我爱我家':{},
},
'回龙观':{},
},
'海淀':{
'五道口':{
'谷歌':{},
'网易':{},
'快手':{},
'SOHU':{},
'SOGO':{},
},
'中关村':{
'YOUKU':{},
'IQIYI':{},
'汽车之家':{},
'新东方':{},
'QQ':{},
},
},
},
'上海':{
'浦东':{
'陆家嘴':{
'CICC':{},
'高盛':{},
'摩根':{},
},
'外滩':{},
},
'闵行':{},
'静安':{}, },
'山东':{
'济南':{},
'德州':{
'乐陵':{
'丁务镇':{},
'城区':{},
},
'平原':{},
},
'青岛':{},
}
} current_layer = menu#实现动态循环
parent_layers = []#保存所有父级,最后一个元素永远是父级
while True:
for key in current_layer:
print(key)
choice = input('>>>:').strip()
if len(choice) == 0:
continue
if choice in current_layer:
parent_layers.append(current_layer)#在进入下一层之前,把当前层(也就是下一层父级)追加到列表中
#下一次loop,当用户选择b的时候,就可以直接去列表中最后一个值
current_layer = current_layer[choice]#改成了子层
elif choice == 'b' and parent_layers:
current_layer = parent_layers.pop()#取出列表中的最后一个值,因为他是当前层的父级
else:print('无此项')
最新文章
- 参数命令化 防止Sql注入
- JS组件系列——Bootstrap Select2组件使用小结
- Oracle删除所有表
- DIV------使用 <;div>; 元素的网页布局
- ubuntu 12.04 安装nginx
- UVA 712-S-Trees(满二叉树的简单查询)
- windows7下virtualBox配置识别usb
- springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来
- 【easy】572. Subtree of Another Tree
- 分布式session解决——Spring-data-redis
- Delphi使用两种不同方法获取系统端口信息--(装载)
- Java爬虫框架Jsoup学习记录
- Linux环境下使用tcpdump抓包与下载
- Linux Centos下安装jdk
- 第一章:HTML5的基础
- java.io.Serializable中serialVersionUID的作用
- U3D的有限状态机系统
- django 项目中使用多数据库 multiple databases
- python第三方库——xlrd和xlwt操作Excel文件学习
- 第三次博客作业JSF