python答案添加

Posted byhsd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python答案添加相关的知识,希望对你有一定的参考价值。

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

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答案添加的主要内容,如果未能解决你的问题,请参考以下文章

Python将“ u”添加到字符串中

智慧树知到《Python语言应用(西安外国语大学)》2022章节测试答案

Python动态添加按钮分别管理[重复]

如何将 python 模块添加到 jython/lib?

在Python方法上添加Timer [重复]

Python数据相关[关闭]