鱼C论坛

 找回密码
 立即注册
查看: 1936|回复: 3

[已解决]求赐教~~~身份证校验

[复制链接]
发表于 2019-11-21 23:41:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
身份证有效性校验。一个合法的中国居民身份证号码有18位,其中前6位为地区编号,第7-14位为出生日期,第15-17位为登记流水号,其中第17位是偶数为女性,是奇数则为男性,第18位为校验码。校验码的生成规则请见后面的备注。
请设计编写程序,输入18位身份证,判断其合法性,此程序我们判断两个方面的合法性,

一是长度是否是18位,并且前17位均为数字,如果不合法请给出相应提示;

二是判断最后一位校验码是否正确(注意处理字母大小写的问题),如果不合法请给出相应提示;不判断地区、生日的合法性。

如果输入的身份证非法,请用户重新输入,若输入3次仍为非法的身份证号,则结束程序,输出“输入的身份证号无法识别”。如果身份证有效,判断持有人的性别,并输出“身份证有效,持有人的性别为XX”,程序结束。能用函数实现的最好多写函数。

提示:可定义两个元组 factor、last。
factor= (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2)
last=(“1”,”0”,”x”,”9”,”8”,”7”,”6”,”5”,”4”,”3”,”2”)

备注:身份证的校验码生成规则:
将身份证17位本体码各位数字乘以对应加权因子并求和,除以11得到余数,根据余数通过校验码对照表查得校验码。

万分感谢!!! w.png y.png
最佳答案
2019-11-22 05:25:28
你好,时间仓促,没有特别测试。
有问题欢迎追问
  1. '''中国居民身份证号码合法性检验'''


  2. def length_check(numbers):
  3.     if len(numbers) == 18:
  4.         return True
  5.     else:
  6.         return False


  7. def factors_check(factors):
  8.     return ''.join(factors).isdigit()


  9. def checkcode_check(factors, last):
  10.     sum = int(factors[0]) * 7 + int(factors[1]) * 9 + int(factors[2]) * 10 + int(factors[3]) * 5 + \
  11.           int(factors[4]) * 8 + int(factors[5]) * 4 + int(factors[6]) * 2 + int(factors[7]) * 1 + \
  12.           int(factors[8]) * 6 + int(factors[9]) * 3 + int(factors[10]) * 7 + int(factors[11]) * 9 + \
  13.           int(factors[12]) * 10 + int(factors[13]) * 5 + int(factors[14]) * 8 + int(factors[15]) * 4 + \
  14.           int(factors[16]) * 2
  15.     right = sum % 11
  16.     if last == 'X' or last == 'x':
  17.         if right == 2:
  18.             return True
  19.         else:
  20.             return False
  21.     else:
  22.         if right == int(last):
  23.             return True
  24.         else:
  25.             return False


  26. def output(ID_numbers):
  27.     if int(ID_numbers[16]) % 2 == 0:
  28.         gender = "女"
  29.     else:
  30.         gender = "男"
  31.     print("身份证有效,持有人的性别为" + gender)


  32. def legal_check():
  33.     times = 0
  34.     legality_flag = True
  35.     while times < 3:
  36.         legality_flag = True    # True represents that it is legal.
  37.         times += 1
  38.         ID_numbers = input("请输入您的18位身份证号: ")
  39.         ID_numbers = list(ID_numbers)
  40.         factors = ID_numbers[:-1]
  41.         last = ID_numbers[-1]
  42.         # check whether the length is 18 digits
  43.         if not length_check(ID_numbers):
  44.             legality_flag = False
  45.             print("输入的号码总长度不为18.")
  46.             continue
  47.         # check whether the factors are all numbers
  48.         if not factors_check(factors):
  49.             legality_flag = False
  50.             print("前17位不全是数字。")
  51.             continue
  52.         # check whether the last check code is read correctly
  53.         if (last != 'X' and last != 'x') and not last.isdigit():
  54.             legality_flag = False
  55.             print("最后一位应为数字,或'X'、'x'.")
  56.             continue
  57.         # check the legality of the checkcode
  58.         if not checkcode_check(factors, last):
  59.             legality_flag = False
  60.             print("最后一位校验码不正确")
  61.             continue

  62.         if legality_flag:
  63.             output(ID_numbers)
  64.             break

  65.     if not legality_flag:
  66.         print("输入的身份证号无法识别")


  67. legal_check()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-22 05:25:28 | 显示全部楼层    本楼为最佳答案   
你好,时间仓促,没有特别测试。
有问题欢迎追问
  1. '''中国居民身份证号码合法性检验'''


  2. def length_check(numbers):
  3.     if len(numbers) == 18:
  4.         return True
  5.     else:
  6.         return False


  7. def factors_check(factors):
  8.     return ''.join(factors).isdigit()


  9. def checkcode_check(factors, last):
  10.     sum = int(factors[0]) * 7 + int(factors[1]) * 9 + int(factors[2]) * 10 + int(factors[3]) * 5 + \
  11.           int(factors[4]) * 8 + int(factors[5]) * 4 + int(factors[6]) * 2 + int(factors[7]) * 1 + \
  12.           int(factors[8]) * 6 + int(factors[9]) * 3 + int(factors[10]) * 7 + int(factors[11]) * 9 + \
  13.           int(factors[12]) * 10 + int(factors[13]) * 5 + int(factors[14]) * 8 + int(factors[15]) * 4 + \
  14.           int(factors[16]) * 2
  15.     right = sum % 11
  16.     if last == 'X' or last == 'x':
  17.         if right == 2:
  18.             return True
  19.         else:
  20.             return False
  21.     else:
  22.         if right == int(last):
  23.             return True
  24.         else:
  25.             return False


  26. def output(ID_numbers):
  27.     if int(ID_numbers[16]) % 2 == 0:
  28.         gender = "女"
  29.     else:
  30.         gender = "男"
  31.     print("身份证有效,持有人的性别为" + gender)


  32. def legal_check():
  33.     times = 0
  34.     legality_flag = True
  35.     while times < 3:
  36.         legality_flag = True    # True represents that it is legal.
  37.         times += 1
  38.         ID_numbers = input("请输入您的18位身份证号: ")
  39.         ID_numbers = list(ID_numbers)
  40.         factors = ID_numbers[:-1]
  41.         last = ID_numbers[-1]
  42.         # check whether the length is 18 digits
  43.         if not length_check(ID_numbers):
  44.             legality_flag = False
  45.             print("输入的号码总长度不为18.")
  46.             continue
  47.         # check whether the factors are all numbers
  48.         if not factors_check(factors):
  49.             legality_flag = False
  50.             print("前17位不全是数字。")
  51.             continue
  52.         # check whether the last check code is read correctly
  53.         if (last != 'X' and last != 'x') and not last.isdigit():
  54.             legality_flag = False
  55.             print("最后一位应为数字,或'X'、'x'.")
  56.             continue
  57.         # check the legality of the checkcode
  58.         if not checkcode_check(factors, last):
  59.             legality_flag = False
  60.             print("最后一位校验码不正确")
  61.             continue

  62.         if legality_flag:
  63.             output(ID_numbers)
  64.             break

  65.     if not legality_flag:
  66.         print("输入的身份证号无法识别")


  67. legal_check()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-22 07:52:52 | 显示全部楼层
  1. factor= (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
  2. check_code = ('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2')

  3. def base_check(str_id):
  4.     if len(str_id) != 18:
  5.         print("输入的号码总长度不为18.")
  6.         return False
  7.     elif not str_id[:17].isdigit():
  8.         print("前17位不全是数字。")
  9.         return False
  10.    
  11.     return True

  12. def identity_check(str_id):
  13.     sum = 0
  14.     for index, num in enumerate(str_id[:17]):
  15.         sum += int(num) * factor[index]

  16.     mod = sum % 11
  17.     return str_id[17].lower() == check_code[mod]

  18. def main():
  19.     times = 3
  20.     while times:        
  21.         ID_numbers = input("请输入您的18位身份证号: ")
  22.         
  23.         if not base_check(ID_numbers):
  24.             times -= 1
  25.             continue      
  26.         elif not identity_check(ID_numbers):            
  27.             print("最后一位校验码不正确")
  28.             times -= 1
  29.             continue
  30.         else:
  31.             break      

  32.     if not times:
  33.         print("输入的身份证号无法识别")
  34.     else:
  35.         sex= '男' if int(ID_numbers[16]) % 2 != 0 else '女'
  36.         print("身份证有效,持有人的性别为{0}".format(sex))

  37. if __name__ == '__main__':
  38.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-28 07:55:55 | 显示全部楼层
码了这么多代码,也没混上个最佳答案,真是悲哀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 07:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表