#!/usr/bin/env python # _authon bruce # @date: 2017/09/09 15:47 menu = { "江西":{ "贛州":{ "興國":{}, "於都":{} }, "南昌":{}, "吉安":{} }, "上海"... ...
#!/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("無此項")