码迷,mamicode.com
首页 > 编程语言 > 详细

python答案添加

时间:2019-07-09 12:15:35      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:key   car   上海   pytho   get   调用   程序   span   河南   

#任务一:处理身份证号判断是否格式正确

def judge_idCard(idCard):
    import re
    if len(idCard)== 18:
        number = re.compile([1-8][0-9]{5}(19[0-9]{2}|20[0-9]{2})((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$)
        if (re.match(number, idCard)):
            return True
        else:
            print(身份证件不可以有多个字母以及多个特殊字符,请输入正确格式的身份证号哦!)
            return False
    else:
        print("身份证一共有18位哦!")
        return False

#任务二:获取号码所在的省份信息
def getProvince(idCard):
    area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林", "23": "黑龙江", "31": "上海",
          "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北", "43": "湖南",
          "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州", "53": "云南", "54": "西藏", "61": "陕西",
          "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆", "71": "台湾", "81": "香港", "82": "澳门"}
    key = idCard[0: 2]  
    if key in area.keys():
        if (not area[(idCard)[0:2]]):
            return "省份信息错误"
        else:
            return area[(idCard)[0:2]]
    else:
        return "省份代码错误"

#任务三:获得号码中的性别信息
def getSex(idCard):
    if int(idCard[16])%2 == 0:
        sex = ""
    else:
        sex = ""
    return sex

#任务四:获取号码中的年龄信息
def getAge(idCard):
    import datetime 
    currentYear = datetime.datetime.now().year
    userAge = int(currentYear) - int(idCard[6:9])
    return userAge

#任务五:组装函数,获得信息
def input_data():
    """程序主体"""
    idCard = input("请输入查找的身份证号码: ")
    is_true = judge_idCard(idCard)
    if is_true:
        province = getProvince(idCard)
        sex = getSex(idCard)
        age = getAge(idCard)
        print(所在省份:,province)
        print(性别:,sex)
        print(年龄:,age)
        input_data()
    else:
        input_data()

# 调用函数,运行程序
if __name__ == "__main__":
    input_data()







#任务一:处理身份证号判断是否格式正确

def judge_idCard(idCard):
    import re
    if len(idCard)== 18:
        number = re.compile([1-8][0-9]{5}(19[0-9]{2}|20[0-9]{2})((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$)
        if (re.match(number, idCard)):
            return True
        else:
            print(身份证件不可以有多个字母以及多个特殊字符,请输入正确格式的身份证号哦!)
            return False
    else:
        print("身份证一共有18位哦!")
        return False

#任务二:获取号码所在的省份信息
def getProvince(idCard):
    area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林", "23": "黑龙江", "31": "上海",
          "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北", "43": "湖南",
          "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州", "53": "云南", "54": "西藏", "61": "陕西",
          "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆", "71": "台湾", "81": "香港", "82": "澳门"}
    key = idCard[0: 2]  
    if key in area.keys():
        if (not area[(idCard)[0:2]]):
            return "省份信息错误"
        else:
            return area[(idCard)[0:2]]
    else:
        return "省份代码错误"

#任务三:获得号码中的性别信息
def getSex(idCard):
    if int(idCard[16])%2 == 0:
        sex = ""
    else:
        sex = ""
    return sex

#任务四:获取号码中的年龄信息
def getAge(idCard):
    import datetime 
    currentYear = datetime.datetime.now().year
    userAge = int(currentYear) - int(idCard[6:9])
    return userAge

#任务五:组装函数,获得信息
def input_data():
    """程序主体"""
    idCard = input("请输入查找的身份证号码: ")
    is_true = judge_idCard(idCard)
    if is_true:
        province = getProvince(idCard)
        sex = getSex(idCard)
        age = getAge(idCard)
        print(所在省份:,province)
        print(性别:,sex)
        print(年龄:,age)
        input_data()
    else:
        input_data()

# 调用函数,运行程序
if __name__ == "__main__":
    input_data()

 

 

 

python答案添加

标签:key   car   上海   pytho   get   调用   程序   span   河南   

原文地址:https://www.cnblogs.com/byhsd/p/11156407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!