题目描述
请写代码校验第二代身份证号码有效性。程序接收一个18位的身份证号码和性别,根据以下规则输出号码是有效还是无效。
第二代身份证号组成规则:
a) 身份证号码(18位)= 地址码(6)+ 出生日期码(8)+ 顺序码(3)+校验码(1);
b) 地址码:保证位数合法即可,无需校验合法性;
c) 出生日期码:格式为YYYYMMDD,需校验日期有效性;
d) 顺序码:男性为奇数,女性为偶数;
e) 校验码:
S = ∑(i = 1, 17) { A[i] * W[i] }
Y = S % 11
校验码 N = (12 - Y) % 11
所以N取值范围是0-10,10在身份证号码中用大写字母’X’表示
i:表示号码字符从左至右不包括校验码字符在内的位置序号
A[i]:表示第i位置上的身份证号码字符值
W[i]:表示第i位置上的加权系数,其数列为7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
输入
110101199003071938 Male
idnum,sex=input().split()
b=list(idnum)
date = b[6:14]
year = date[0:4]
year1 = int(''.join(year))
month = date[4:6]
month1 = int(''.join(month))
day = date[6:8]
day1 = int(''.join(day))
sexnum = b[14:17]
sexnum1 = int(''.join(sexnum))
a=b[0:17]
a1=list(map(int,a))
w1=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
s=0
for i in range(0,17):
s+=a1[i]*w1[i]
y=s%11
n=(12-y)%11
if n==10:
n1='X'
else:
n1=str(n)
monthlist1=[31,28,31,30,31,30,31,31,30,31,30,31]
monthlist2=[31,29,31,30,31,30,31,31,30,31,30,31]
if year1%4 == 0 and year1%100 != 0 or year1%400 == 0:
monthlist=monthlist2
else:
monthlist=monthlist1
if len(idnum)==18 and (sex=='Female'or sex=='Male'):
if year1>=0 and year1<=2020 and month1>= 1 and month1 <= 12:
if day1>=1 and day1<= monthlist[month1-1]:
if n1==b[17]:
if (sexnum1 %2==0 and sex=='Female')or(sexnum1%2!=0 and sex=='Male'):
print('Pass')
else:
print('Fail')
else:
print('Fail')
else:
print('Fail')
else:
print('Fail')
else:
print('Fail')