7.初探python之简单三级菜单

# -*- coding: utf-8 -*-
# @Time : 2018/8/6 8:31
# @Author : Rolland
# @File : dict&str.py
# @Software: PyCharm

menu = {
'中国':{
'广东':{
'广州':{
'网易游戏':{},
'酷狗音乐':{},
'爱拍科技':{},
},
'佛山':{
'亿达信息':{},
'林氏木业':{},
'众图互联':{},
},
'中山':{
'鼎捷软件':{},
'联邦制药':{},
'虹美公司':{},
},
},
'广西':{
'桂林':{},
'南宁':{},
'北海':{},
},
'海南':{
'海口':{},
'三亚':{},
},
},
'美国':{

},
'俄罗斯':{

},
}
back_flag = False
quit_flag = False
while not back_flag and not quit_flag:
for country in menu:
print(country)
choice = input('请选择国家:').strip()
if choice in menu:
while not back_flag and not quit_flag:
for province in menu[choice]:
print(province)
choice2 = input('请选择省份:').strip()
if choice2 == 'b':
back_flag = True
if choice2 == 'q':
quit_flag = True
if choice2 in menu[choice]:
while not back_flag and not quit_flag:
for city in menu[choice][choice2]:
print(city)
choice3 = input('请选择城市:').strip()
if choice3 == 'b':
back_flag = True
if choice3 == 'q':
quit_flag = True
if choice3 in menu[choice][choice2]:
while not back_flag and not quit_flag:
for company in menu[choice][choice2][choice3]:
print(company)
choice4 = input('请选择:').strip()
if choice4 == 'b':
back_flag = True
if choice4 == 'q':
quit_flag = True
else:
back_flag = False
else:
back_flag = False
else:
back_flag = False




升级版:
# -*- coding: utf-8 -*-
# @Time : 2018/8/6 8:31
# @Author : Rolland
# @File : dict&str.py
# @Software: PyCharm

menu = {
'中国':{
'广东':{
'广州':{
'网易游戏':{},
'酷狗音乐':{},
'爱拍科技':{},
},
'佛山':{
'亿达信息':{},
'林氏木业':{},
'众图互联':{},
},
'中山':{
'鼎捷软件':{},
'联邦制药':{},
'虹美公司':{},
},
},
'广西':{
'桂林':{},
'南宁':{},
'北海':{},
},
'海南':{
'海口':{},
'三亚':{},
},
},
'美国':{

},
'俄罗斯':{

},
}

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)
current_layer = current_layer[choice]
elif choice == 'b':
if parent_layers:
current_layer = parent_layers.pop()
else:
print('无')

猜你喜欢

转载自www.cnblogs.com/zhoupeng-L/p/9440781.html