menu = {"北京":{"海淀":["知春路","公主坟","西土城","牡丹园"],"朝阳":["金台夕照","望京","青年路","三元桥"],"昌平":["天通苑","立水桥","沙河","西二旗"]},"哈尔滨":{"江北":["宾县","方正县","依兰县"],"南岗":["巴彦县","木兰县","延寿县","通河县"]
},"上海":{"杨浦区":["黄浦区","静安区","长宁区"],"虹口区":["卢湾区","徐汇区","闵行区"],"普陀区":["浦东新区","宝山区","松江区"]}}
current_layer = menu # 存放当前节点
pre_layer=[] #存放前面的节点数据
while True:
for i in current_layer:
print(i)
user_opt = input(">>:").strip()
if user_opt == None:
continue
elif user_opt == "q":
exit()
elif user_opt == "b":
if current_layer == menu:#若当前节点已是最顶层节点,则不回退
continue
else:
current_layer = pre_layer.pop()#回退一级,上一级节点变成当前节点
elif user_opt in current_layer:
pre_layer.append(current_layer)#前进一级,当前节点变为上一级节点
current_layer = current_layer[user_opt]#下一级节点变为当前节点