一定要把python学好!至少入门py以后慢慢建立计算机体系,我比科班出身的人落后了四年甚至更多!必须付出更多时间和精力来学习!
作业要求
给出如下的嵌套字典:
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
1.先在屏幕上打印出字典的一级菜单的key
2.用户可以输入key进入所选择的下一级菜单
3.用户输入'b'可返回上一级菜单
4.用户输入'q'退出程序
5.第一级按'b'会询问是否退出
源代码
#author : negu
# -*- coding: utf-8 -*-
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{},
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{"oldboy","test"},
'北航':{},
},
'天通苑':{"链家地产","我爱我家"},
'回龙观':{},
},
'朝阳':{"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{},
}
},
'闸北':{
'火车战':{
'携程':{},
}
},
'浦东':{},},
'山东':{ },
'广东':{
"东莞":{},
"常熟":{},
"佛山":{},}
}
exit_flag = False
while not exit_flag:#多个while-if嵌套实现菜单的返回功能
for i in menu:
print(i)
choice = input("选择进入1>>")
if choice in menu:
while not exit_flag:
for i2 in menu[choice]:
print("\t",i2)
choice2 = input("选择进入2>>:")
if choice2 in menu[choice]:
while not exit_flag:
for i3 in menu[choice][choice2]:
print("\t\t",i3)
choice3 = input("选择进入3>>:")
if choice3 in menu[choice][choice2]:
for i4 in menu[choice][choice2][choice3]:
print("\t\t",i4)
choice4 = input("最后一层,按b返回>>4:")
if choice4 == 'b':
pass #让代码不出错...只是一个占位符.,跳出最后一层循环
elif choice4 == 'q':
exit_flag = True#flag编程true退出程序
if choice3 == 'b':
break#往上跳级
elif choice3 == 'q':
exit_flag = True
if choice2 == 'b':
break
elif choice2 == 'q':
exit_flag = True
elif choice == 'b':
print('已经是第一层,按y退出,按其他任意键返回。 ')
choice_1 = input('>>')
if choice_1 == 'y':
pass
else:continue
break
elif choice == 'q':
exit()
测试方法
1.进入以后直接按b
2.进入北京-海淀-五道口-soho后返回到一级菜单
3.按q退出程序
测试结果
广东
北京
上海
山东
选择进入1>>北京
朝阳
海淀
昌平
东城
选择进入2>>:海淀
上地
五道口
中关村
选择进入3>>:五道口
google
soho
网易
最后一层,按b返回>>4:b
上地
五道口
中关村
选择进入3>>:b
朝阳
海淀
昌平
东城
选择进入2>>:b
广东
北京
上海
山东
选择进入1>>q
Process finished with exit code 0
大神的代码
大神的代码利用了列表的pop命令自动删除列表最后一项数据,以及for循环字典的时候只提取最外层的key。利用字典和列表功能实现了菜单项目。代码精简,给我的启发很大。就是在第一层输入'b'会出错。我优化了一下。将优化后的代码贴出来瞻仰一下。
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
exit_flag = False
current_layer = menu #默认为菜单字典
layers = [menu]#这是一个列表,一开始第一个数据为整个字典
while not exit_flag:
for k in current_layer:#打印现有层数的所有key
print(k)
choice = input(">>:").strip()#输入层数
if choice == "b":
if len(layers) == 0:#判断是否是第一层
print("it's first layer!!!")
continue
current_layer = layers[-1]#将列表中的最后一个值赋值给current_layer
#print("change to laster", current_layer)
layers.pop()#赋值完即删除
elif choice not in current_layer:continue
elif choice == 'q':exit()
else:
layers.append(current_layer)#输入进入下一层的名称
current_layer = current_layer[choice]