思路
主要为两层检验:
第一层:身份证前17位是否皆为数字(将输入字符直接转为int,若抛出异常,则为不合格,没有则合格)
第二层:对前17位加权求和模11时,与检验码是否一致。(通过计算结果判断)
代码:
weight=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
check={0:'1',1:'0',2:'X',3:'9',4:'8',5:'7',6:'6',7:'5',8:'4',9:'3',10:'2'} #z:M
card_num=int(input())
pass_count=0
for i in range(card_num):
card=input()#.split(' ')
try:
#a =int(card[0:17])
sum=0
for i ,j in zip(card[0:17],weight):
sum=sum+int(i)*j
Z=sum%11 #计算的和对11取模得到值Z
if(check[Z]==card[17]):
pass_count=pass_count+1
else:
print(card) #
continue
except Exception as e:
print(card) # print(card)
continue
if pass_count==card_num:
print('All passed')