身份证号有效性检验代码 (python)
身份证号码有效性检验:
输入:str类型的身份证号码
返回值: bool
# 身份证号有效性验证
def id_number_valid(id_number):
"""
# 身份证号有效性验证
:param id_number: str类型
:return:
"""
# 输入参数字符串
total_sum = 0
multiply_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
valid = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
for index in range(len(id_number)-1):
total_sum = total_sum + int(id_number[index])*multiply_factor[index]
result = total_sum % 11
return valid[result] == id_number[-1]
if __name__ == "__main__":
number = '******************' # 要检测的身份证号码
if id_number_valid(number):
print("Valid ID Number")
else:
print("Invalid ID Number")