三级菜单
作业需求:
数据结构:
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车站':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
需求:
可依次选择进入各子菜单
可从任意一层往回退到上一层
可从任意一层退出程序
所需新知识点:列表、字典
作业效果展示:
三级菜单效果展示
踩分点:
基础需求:80%
- 可依次选择进入各子菜单
- 可从任意一层往回退到上一层
- 可从任意一层退出程序
所需新知识点:列表、字典
升级需求:10%
- 使用一个while循环,且整体代码量不超过15行
编码规范需求:10% - 代码规范遵守pep8 (https://python.org/dev/peps/pep-0008/)
- 程序的说明文档(https://github.com/csrftoken/surveySystem)
- 程序的说明文档必须包含的内容:程序的实现的功能、程序的启动方式、登录用户信息、程序的运行效果
- 程序流程图(使用图形表示程序执行过程的一种思路):
可以参考 https://www.processon.com/view/link/589eb841e4b0999184934329
Python 作业评分标准说明,满分100分:
- 作业需求所有的功能占90%(如果有升级需求:基本需求85%,升级需求5%)。
- PEP8代码规范(代码简洁)5%。
- 说明文档(readme)、流程图(画出程序的业务/执行流程) 5%。
以上三点综合对作业进行评分【禁止提交txt文档】。

menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车站': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
}
for key in menu:
print(key)
while True:
input_key_0 = input(">>:")
if input_key_0 in menu:
for key_1 in menu[input_key_0]:
print(key_1)
elif input_key_0 == 'b':
break
elif input_key_0 == 'q':
print('再见')
exit()
else:
print("输入错误")
continue
while True:
input_key_1 = input(">>:")
if input_key_1 in menu[input_key_0]:
for key_2 in menu[input_key_0][input_key_1]:
print(key_2)
elif input_key_1 == 'b':
for key in menu:
print(key)
break
elif input_key_1 == 'q':
print('再见')
exit()
else:
print("输入错误")
continue
while True:
input_key_2 = input(">>:")
if input_key_2 in menu[input_key_0][input_key_1]:
for key_3 in menu[input_key_0][input_key_1][input_key_2]:
print(key_3)
elif input_key_2 == 'b':
for key_1 in menu[input_key_0]:
print(key_1)
break
elif input_key_2 == 'q':
print('再见')
exit()
else:
print("输入错误")
continue
while True:
input_key_3 = input(">>:")
if input_key_3 in menu[input_key_0][input_key_1][input_key_2]:
print(input_key_3, "已达最底层")
elif input_key_3 == 'b':
for key_2 in menu[input_key_0][input_key_1]:
print(key_2)
break
elif input_key_3 == 'q':
print('再见')
exit()
else:
print("输入错误")
continue
再来对比一下标准答案吧
current_layer = menu
layers = []
while True:
for i in current_layer:
print(i)
choice = input(">>:").strip()
if not choice:
continue
if choice in current_layer:
layers.append(current_layer)
current_layer = current_layer[choice]
elif choice == 'b':
if len(layers) != 0:
current_layer = layers.pop()
else:
print("已是顶层")
elif choice == 'q':
exit()
有些空格被删除掉了,还是用图吧:


浙公网安备 33010602011771号