省市县

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/5/16 10:43
# @File    : shengshi.py


dic = {
    "河北":{
        "石家庄" :["鹿泉","藁城","元氏"],
        "邯郸" : ["永年","涉县","磁县"]
    },
    "河南":{
        "郑州":["巩义","登封","新密"],
        "开封":["金明","鼓楼","通许"]
    },
    "山西":{
        "太原":["古交","清徐","阳曲"],
        "大同":["天镇","阳高","广灵"]
    }
}
###定义颜色
def get_color(text, color='red'):
    """
    将字符串格式化为带颜色内容
    :param text: 接收字符串
    :param color:默认红色
    :return: ->str 返回带颜色的内容
    """
    color_table = {
        "red": "\033[31m",
        "green": "\033[32;1m",
        "yellow": "\033[33;1m",
        "purple": "\033[36;1m",
        "end": "\033[0m",  # 结束符号
    }
    li = [color_table.get(color), str(text), color_table["end"]]
    return ''.join(li)

while True:
    province = input('输入Q结束,输入你喜欢的省:')
    if len(province) == 0:
        print("不能为空!")
        continue
    elif province == 'Q':
        exit()
    elif province not in dic:
        print('天朝的省份都不知道,打死你哥龟孙!!!')
        continue
    city_list=[]
    for i in dic[province]:
        city_list.append(i)
    print(city_list)
    print(get_color('%s省包括:%s' % (province, city_list), 'green'))

    while True:
        city = input("输入Q结束:输入q返回,输入城市查看县:")
        if city == 'q':
            break
        elif city == 'Q':
            exit()
        elif city in city_list:             #必须放倒最后否则下面的while不会被执行
            township = dic[province][city]
            print(get_color('%s县包括:%s' % (city,township),'yellow'))

            while True:
                county = input("输入Q结束,输入q返回,输入县进入县:")
                if county == 'q':
                        break
                elif county == 'Q':
                    exit()
                elif county in township:
                    print(get_color(('%s县欢迎您!!!' % county),'purple'))

猜你喜欢

转载自www.cnblogs.com/fmgao-technology/p/9045020.html