python-三级菜单
第一种方法:
# _Author:huang
# date: 2017/11/29
menu = {
'河南': {
'郑州': {
'金水区': {
'东风路': {},
'北三环': {},
'明基路': {}
},
},
'南阳': {
'淅川县': {},
'西峡县': {},
'方程': {},
},
},
'江苏': {
'南京': {
'鼓楼': {},
'栖霞区': {},
'下关区': {}
},
'苏州': {
'城区': {},
'苏州园林': {}
}
},
'北京': {
'昌平区': {
'sohu': {},
'sogo': {}
},
'昭阳区': {
'360': {},
'google': {}
}
}
}
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("无此地名")
第二种方法:
# _Author:huang
# date: 2017/11/28
menu = {
'北京': {
'朝阳': {
'国贸': {
'CICC': {},
'HP': {},
'渣打一行': {}
}
},
'望京': {
'陌陌': {},
'奔驰': {},
'360': {}
},
'三里屯': {
'优衣库': {},
'apple': {}
},
'昌平': {
'天通苑': {
'链家': {},
'我爱我家': {}
},
'回龙观': {},
},
'海淀': {
'五道口': {
'谷歌': {},
'网易': {},
'sohu': {},
'sogo': {},
'快手': {},
},
'中关村': {
'youku': {},
'Iqiyi': {},
"汽车之家": {},
"新东方": {},
'QQ': {}
}
}
},
'上海': {
'浦东': {
'陆家嘴': {
'CICC': {},
'高盛': {},
'摩根': {}
},
'外滩': {},
},
'闵行': {},
'静安': {}
},
'山东': {
'济南': {},
'青岛': {},
'德州': {
'乐陵': {
'丁坞镇': {},
'城区': {}
},
'平原县': {}
}
}
}
back_flag = False
exit_flag = False
while not back_flag and not exit_flag:
for key in menu:
print(key) # 打印第一层代码
choice = input(">>:").strip()
if choice == 'b':
back_flag = True
if choice == 'q':
exit_flag = True
if choice in menu: # 判断是否在第一层
while not back_flag and not exit_flag: #让程序停在第二层
for key2 in menu[choice]:
print(key2) # 打印第二层
choice2 = input("2>>:").strip()
if choice2 == 'b':
back_flag = True
if choice2 == 'q':
exit_flag = True
if choice2 in menu[choice]:
for key3 in menu[choice][choice2]:
print(key3)
choice3 = input("3>>:").strip()
if choice3 == 'b':
back_flag = True
if choice3 == 'q':
exit_flag = True
if choice3 in menu[choice][choice2]:
while not back_flag and not exit_flag:
for key4 in menu[choice][choice2][choice3]:
print(key4)
choice4 = input("4>>:").strip()
print("last lever")
if choice4 == 'b':
back_flag = True
if choice4 == 'q':
exit_flag = True
else:
back_flag = False
else:
back_flag = False
else:
back_flag = False