menu = { "山东": { "济南": { "平阴": {}, "商河": {}, "莱芜": {} }, "济宁": { "金乡": {}, "微山": {}, "嘉祥": {} }, "曲阜": { "南辛": {}, "陵城": {}, "防山": {} } }, "北京": { "海淀": { "万寿路": {}, "永定路": {}, "中关村": {} }, "昌平": { "回龙观": {}, "南口": {}, "沙河": { "路飞":{}, "娜美":{} } }, "朝阳": { "呼家楼": {}, "三里屯": {}, "双井": {} } }, "上海": { "黄浦": { "南京东路": {}, "外滩": {}, "瑞金二路": {} }, "徐汇": { "湖南路": {}, "天平路": {}, "枫林路": {} }, "长宁": { "华阳路": {}, "新华路": {}, "江苏路": {} } }, } def func(m): current_layer = m layers = [] while True: for k in current_layer: #menu[北京] print(k) choice = input(">>:").strip() if not choice:continue if choice in current_layer: layers.append(current_layer)#进入下一层,保存当前层 current_layer = current_layer[choice] #menu[北京][昌平] elif choice == 'b': if len(layers) != 0: current_layer= layers.pop() else: print("已经在最顶层了") elif choice == 'q': print('bye......') break func(menu)