python 省市区字典

dic = {
      "福建省":{"福州市":"['马尾','鼓楼']","厦门":"['湖里','思明']"},
        "广东省":{"深圳":"['深圳1','深圳2']","东莞":"['东莞1','东莞2']"}
      }

while True:
    city_list = []
    province = input("请输入省份:")
    if len(province) == 0:
        print("请输入省份")
        continue
    #当输入'q'退出
    elif province == "q":
        exit()
    elif province not in dic:
        print("输入的省份不符合标准")
        continue
    else:
        for i in dic[province]:
            city_list.append(i)
    print(city_list)
    while True:
        city = input("城市:")
        if city == 'q':
            exit()
        elif city in city_list:
            towm = dic[province][city]
            print("城市:",towm)
        # 当输入'p'返回省份输入
        elif city == 'p':
            print("返回省份")
            break

        while True:
            country = input("请输入县:")
            if country == 'q':
                exit()
            elif country in towm:
                print("县:", country)
            # 当输入'x'返回市输入
            elif country == 'x':
                print("返回市")
                break

# for i in dic['福建省']:
#     city_list.append(i)
# print(city_list)

猜你喜欢

转载自blog.csdn.net/u014450465/article/details/79115969