仍然需要修改:
1 # encoding: utf-8 2 # 用户交互显示类似省市县N级联动的选择 3 # * 允许用户增加内容 4 # * 允许用户查看某一个级别内容 5 6 dic = { 7 "江西": { 8 "萍乡": ["安源", "彭高", "上栗"], 9 "新余": ["良山", "新钢", "兴安岭"], 10 }, 11 "北京": { 12 "大兴区": ["礼贤镇", "魏善庄镇", "北臧村镇"], 13 "昌平区": ["沙河", "化庄", "白浮泉"], 14 }, 15 "福建": { 16 "莆田": ["荔城", "西天尾", "九化山"], 17 "厦门": ["湖里", "思明", "海仓"], 18 } 19 } 20 print("目前可查询的省份:江西、北京、福建") 21 province = input("请输入省份或者输入N新建: ") 22 23 if province == "N": 24 new_province = input("请输入省份:") 25 new_city = input("请输入城市:") 26 new_town = input("请输入城镇信息:") 27 dic[new_province] = {new_city: [new_town]} 28 29 while province not in dic: 30 print("输入错误省份,请从新输入") 31 province = input("请输入省份或者输入N新建: ") 32 33 while province in dic: 34 new_menu = dic[province] 35 for k in new_menu: 36 print(k, end=" ") 37 choice_city = input("请输入城市:") 38 if choice_city not in new_menu: 39 print("错误城市输入,请从新输入: ") 40 continue 41 else: 42 new_menu2 = new_menu.get(choice_city) 43 for i in new_menu2: 44 print(i, end=" ") 45 choice_town = input("请输入城镇信息:") 46 if choice_town in new_menu2: 47 print( 48 "信息输入正确,省份:{},城市:{},城镇:{}".format( 49 province, 50 choice_city, 51 choice_town)) 52 break