用户交互,显示省市县三级联动的选择
用户交互,显示省市县三级联动的选择 要求:用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县 dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], } "河南": { ... } "山西": { ... }
1 dic = { 2 "河北": { 3 "石家庄": ["鹿泉", "藁城", "元氏"], 4 "邯郸": ["永年", "涉县", "磁县"], 5 }, 6 "河南": { 7 ... 8 }, 9 "山西": { 10 ... 11 } 12 } 13 while True: 14 province = input('请输入你想要知道的省份,输入Qt时退出: ').strip() 15 if province not in dic: 16 print('输入不合法,请重新输入') 17 continue 18 elif len(province) == 0: 19 print('输错了,傻!') 20 continue 21 else: 22 for city in dic.get(province): 23 print(city) 24 your_choose = input('请输入你想要知道的市: ').strip() 25 print(dic.get(province).get(your_choose))