#!/usr/bin/env python # _authon bruce # @date: 2017/09/09 15:47 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("无此项")
时间: 2024-10-01 18:18:19