一.需求
用户交互,显示省市县三级联动的选择
要求:用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县
二.流程图
dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], }, "河南": { "郑州":["上街区","二七区","金水区"], "商丘":["永成","民权","宁陵"], }, "山西": { "大同":["大同","左云","广陵"], "太原":["清徐","阳曲","娄烦"], }, } while True: province = input('请输入省份包含的市: ').strip() if province not in dic: print('输入错误') continue elif len(province) == 0: print('输入不正确') continue else: for city in dic.get(province): print(city) your_choose = input('请输入市查看所有的县: ').strip() print(dic.get(province).get(your))