menu_dict = {
‘山东‘ : {
‘青岛‘ : {
‘四方‘:{‘兴隆路‘,‘平安路‘,‘杭州路‘},
‘黄岛‘:{},
‘崂山‘:{}
},
‘济南‘ : {
‘历城‘:{},
‘槐荫‘:{},
‘高新‘:{}
},
},
‘江苏‘ : {
‘苏州‘ : {
‘沧浪‘:{},
‘相城‘:{},
‘平江‘:{}
},
‘南京‘ : {
‘白下‘:{},
‘秦淮‘:{},
‘浦口‘:{}
}
}
}
current_layer = menu_dict
parent_layers = []#把不同层级的字典保存在列表中(列表中嵌套字典)[menu_dict,menu_dict[省],。。。。],到最后一层的时候列表是下面这个样子
#parent_layers[0]={‘山东‘: {‘青岛‘: {‘四方‘: {‘杭州路‘, ‘兴隆路‘, ‘平安路‘}, ‘黄岛‘: {}, ‘崂山‘: {}}, ‘济南‘: {‘历城‘: {}, ‘槐荫‘: {}, ‘高新‘: {}}}, ‘江苏‘: {‘苏州‘: {‘沧浪‘: {}, ‘相城‘: {}, ‘平江‘: {}}, ‘南京‘: {‘白下‘: {}, ‘秦淮‘: {}, ‘浦口‘: {}}}}
#parent_layers[1]={‘青岛‘: {‘四方‘: {‘杭州路‘, ‘兴隆路‘, ‘平安路‘}, ‘黄岛‘: {}, ‘崂山‘: {}}, ‘济南‘: {‘历城‘: {}, ‘槐荫‘: {}, ‘高新‘: {}}}
#parent_layers[2]={‘四方‘: {‘杭州路‘, ‘兴隆路‘, ‘平安路‘}, ‘黄岛‘: {}, ‘崂山‘: {}}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("无此项")