# Multilevel menu menu = { '省1': { '城市11': { '区域111': {}, '区域112': {}, '区域113': {}, '区域114': {} }, '城市12': { '区211': {}, '区212': {}, '区213': {} }, '城市13': { '区311': {}, '区312': {}, '区313': {} }, }, '省2': { '市21': { '区211': {}, '区212': {}, '区213': {} }, '市22': { '区221': {}, '区222': {}, '区223': {} }, '市23': { '区231': {}, '区232': {}, '区233': {} } }, '省3': { '市31': { '区311': {}, '区312': {}, '区313': {} }, '市32': { '区321': {}, '区322': {}, '区323': {} }, '市33': { '区331': {}, '区332': {}, '区333': {} } } } exit_flag = False while not exit_flag: print("---------省----------") for i in menu: print(i) choice1 = input("请输入省:") if choice1 in menu: while not exit_flag: print("----------城市---------") for i in menu[choice1]: print(i) choice2 = input("请输入城市:") if choice2 in menu[choice1]: while not exit_flag: print("-----------区域----------") for i in menu[choice1][choice2]: print(i) choice3 = input("请选择区域:") if choice3 in menu[choice1][choice2]: print("到达底部") elif choice3 == 'b': break elif choice3 == 'q': exit_flag = True else: pass elif choice2 == 'b': break elif choice2 == 'q': exit_flag = True else: pass elif choice1 == 'q': exit_flag = True
# 优化 city_catalog = { '省1': { '市11': { '区111': {}, '区112': {}, '区113': {} }, '市12': { '区121': {}, '区122': {}, '区123': {} }, '市13': { '区131': {}, '区132': {}, '区133': {} } }, '省2': { '市21': { '区211': {}, '区212': {}, '区213': {} }, '市22': { '区221': {}, '区222': {}, '区223': {} }, '市23': { '区231': {}, '区232': {}, '区233': {} } }, '省3': { '市31': { '区311': {}, '区312': {}, '区313': {} }, '市32': { '区321': {}, '区322': {}, '区323': {} }, '市33': { '区331': {}, '区332': {}, '区333': {} } } } # exit_flag = False # while not exit_flag: # print("---------省-----------") # for i in city_catalog: # print(i) # choice = input("Please enter choice:") # if choice in city_catalog: # while not exit_flag: # print("----------市----------") # for i in city_catalog[choice]: # print(i) # choice2 = input("Please enter choice:") # if choice2 in city_catalog[choice]: # while not exit_flag: # print("-----------区----------") # for i in city_catalog[choice][choice2]: # print(i) # choice3 = input("Please enter choice:") # if choice3 in city_catalog[choice][choice2]: # print("------------last-----------") # for i in city_catalog[choice][choice2][choice3]: # print(i) # while not exit_flag: # choice4 = input("Please enter choice:") # if choice4 == 'b': # break # elif choice4 == 'q': # exit_flag = True # elif choice3 == 'b': # break # elif choice3 == 'q': # exit_flag = True # else: # pass # elif choice2 == 'b': # break # elif choice2 == 'q': # exit_flag = True # else: # pass # elif choice == 'b': # break # elif choice == 'q': # exit_flag = True # else: # pass now_choice = city_catalog history = [] exit_flag = False while not exit_flag: for i in now_choice: print(i) choice = input("Please enter choice:") if choice in now_choice: history.append(now_choice) now_choice = now_choice[choice] elif choice == 'b': now_choice = history.pop() elif choice == 'q': exit_flag = True else: pass
dic = { 'k1': { 'k11': { 'k111': 'v111', 'k112': 'v112' }, 'k12': { 'k121': 'v121', 'k122': 'v122' } }, 'k2': { 'k21': {}, 'k22': {} } } # 利用对战实现 li = [dic] while li: for i in li[-1]: print(i) k = input("Please input key:") if k in li[-1].keys() and li[-1][k]: li.append(li[-1][k]) elif k == 'b': li.pop() elif k == 'q': break else: continue