menu = {
'北京': {
'海淀': {
'五道口': {
'soho': {},
'网易': {},
'google': {}
},
'中关村': {
'爱奇艺': {},
'汽车之家': {},
'youku': {},
},
'上地': {
'百度': {},
},
},
'昌平': {
'沙河': {
'老男孩': {},
'北航': {},
},
'天通苑': {},
'回龙观': {},
},
'朝阳': {},
'东城': {},
},
'上海': {
'闵行': {
"人民广场": {
'炸鸡店': {}
}
},
'闸北': {
'火车战': {
'携程': {}
}
},
'浦东': {},
},
'山东': {},
} # 数据结构
now_menu = menu # 记录当前层
menu_list = [] # 记录路径
while True:
for k in now_menu: # 打印当前层
print(k)
choice = input('请输入菜单,例如:北京 (b回到上层,q退出):').strip() # 用户输入
if choice in now_menu: # 用户输入的值在当前层的key里
if len(menu_list) == 0 or now_menu != menu_list[-1]: # 路径是空或者当前层不是路径里的最后一层,则加到路径里,
menu_list.append(now_menu) # 是为了防止在最底层时重复输入,重复加入到路径里。
now_menu = now_menu[choice] # 进入下一层
if not now_menu: # 如果当前层是空字典,说明是最底层
print('已经是最底层了,请回到上层或退出!')
now_menu = menu_list.pop() # 把上一层拿出来打印
elif choice == 'b': # 回到上层
if len(menu_list) == 0: # 如果路径是空,说明是最上层
print('已经是最上层,请重新输入!')
else:
now_menu = menu_list.pop() # 不是最上层,那就把上层拿出来打印
elif choice == 'q': # 退出
exit('bye')
else:
print('请输入正确值!') # 非法输入,则重新循环
wechat: nick753159 qq: 417966852 email: nzq42@qq.com base: shanghai