menu={'Shanghai':
{'Minhang':
{'zed':{'Age':20},'youku':{}},
'Xuhui':
{'nsn':{}},
'Pudong':
{'zte':{}}
},
'Beijing':
{'Chaoyang':
{'microsoft':{}},
'Haidian':
{'Qinghua':{}},
'Dongcheng':
{'Beida':{}}
},
'Hebei':
{'Handan':
{'Chengan':{'company':'CMCC'},'Feixiang':{}},
'Shijiazhuang':
{'HebeiUniversity':{}},
'Qinhuangdao':
{'YanshanUniversity':{}}
},
}
'''第二步: 逐层菜单的代码实现'''
while True:
str='欢迎进入省市区三级菜单系统'
print(str.center(100, '^'))
for k in menu:
print(k)
choice1=input('选择进入一级菜单|按q退出系统>:')
if not choice1: continue
elif choice1 in menu:
while True: # 进入第二层菜单
for k in menu[choice1]:
print(k)
choice2=input("选择进入二级菜单|按q退出系统|按b回到上一层>>:")
if not choice2:continue
elif choice2 in menu[choice1]:
while True: # 进入第三层菜单
for k in menu[choice1][choice2]:
print(k)
choice3=input('选择进入三级菜单|按q退出系统|按b回到上一层>>>:')
if not choice3:continue
elif choice3 in menu[choice1][choice2]:
print('go to',menu[choice1][choice2][choice3])
elif choice3 == 'b':
break
elif choice3 == 'q':
exit()
else:
print('节点不存在!')
elif choice2 == 'b':
break
elif choice2 == 'q':
exit()
else:
print('节点不存在!')
elif choice1 == 'b':
break
elif choice1 == 'q':
exit()
else:
print('节点不存在!')
'''三级菜单的高级写法'''
current_menu=menu
last_menu=[]
while True:
for k in current_menu:
print(k)
choice=input('>:')
if not choice: continue
elif choice in current_menu:
last_menu.append(current_menu)
print(last_menu)
current_menu=current_menu[choice]
elif choice == 'b':
if len(last_menu)!=0:
current_menu=last_menu.pop()
else:
print('已经是顶层!')
elif choice == 'q':
exit()
else:
print('节点不存在!')