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.

Recommended Answers

All 2 Replies

Provided your code is correct, this is how I could clean it up (unchecked):

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
    ID_code = ('00'+ID_code)[-10:]
    intlist = [int(i) for i in ID_code]
    control = sum(intlist[ind] * (10 - ind) for ind in range(9)) % 11
    return  (control if control < 2 else (11 - control)) == intlist[-1]

print ID_check(raw_input("Enter Your ID Code Number: "))

Thanks pyTony.
it is now far more better.
just as a suggestion, I think it can be a simple project for beginners. if you agree with me, please state this project in a better English for other beginners. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.