Assuming that every person has a National ID number, I am trying to check if the
Entered number is a valid ID number, under these conditions:
1. ID number must be of 10 digits length.
2. If ID length is greater than/equal to 8 and less than 10 digits, add one or two 0s to the left.
3. ID's digits must not be all the same(e.g. '1111111111', '222222222',...
4. Multiply the first 9 digits by numbers 10 to 2 respectively,
add them up and devide the sum by 11:
4.1.if the reminder is less than 2: the reminder must be equal to the last ID digit
4.2.if the reminder is equal to/greater than 2, subtract it from 11:the reminder must be equal to the last ID digit
if any condition is not met, the ID number is INVALID.
this is my effort:
def ID_check(ID_code):
if (all(x==ID_code[0] for x in ID_code)) or len(ID_code)<8 or len(ID_code)>10 :
return False
if 8<=len(ID_code)<10:
ID_code = (10- len(ID_code))*'0'+ID_code
intlist = [int(i) for i in ID_code]
control = (intlist[0]*10+intlist[1]*9+intlist[2]*8+intlist[3]*7+intlist[4]*6+intlist[5]*5+intlist[6]*4+intlist[7]*3+intlist[8]*2)%11
if control<2:
return control == intlist[9]
elif control >= 2:
control = 11 - control
return control == intlist[9]
print ID_check(raw_input("Enter Your ID Code Number: "))
Any Suggestion/Correction is appreciated.
P.S. Sorry for my English, Its not my First language.