Python 城市列表

要求:用户输入河北,则打印河北省下的市,用户输入市,则显示该河北省的这个市下的县

dic = {
    "河北": {
        "石家庄": ["鹿泉", "藁城", "元氏"],
        "邯郸": ["永年", "涉县", "磁县"],
    },
    "北京": {
        "昌平":["沙河","回龙观","龙泽"],
        "海淀":["中关村","五道口","知春路"]
    },
    "山西": {
        "太原":["清徐县","阳曲县","娄烦县"],
        "大同":["大同县","天镇县","浑源县"]
    }
}

while True:
    city=input("请输入想要查看的城市:")
    if city not in dic:
        print("输入有误,请重新输入:")
        continue
    print(dic[city])
    xiancheng=input("请输入想要查看的县或区:")
    if xiancheng not in dic[city]:
        print("输入有误,请重新输入")
        continue
    print(dic[city][xiancheng])
    if city == "exit" or xiancheng=="exit":
        exit()
View Code


 
原文地址:https://www.cnblogs.com/zhangxinxiao/p/7169134.html