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.

Dani AI

Generated

Nice start, The checksum rule you described is solid. Two small pitfalls to watch for: (1) run the "all digits the same" test after you normalize to 10 digits, and (2) sanitize input so stray spaces or separators do not break validation. ’s cleanup heads in that direction; below is a defensive, Python-3-friendly version that keeps intent clear and preserves leading zeros.

def is_valid_id(s: str) -> bool:
    # keep only ASCII digits
    s = ''.join(ch for ch in s.strip() if '0' <= ch <= '9')
    if not (8 <= len(s) <= 10):
        return False

    # left-pad to 10 digits
    s = s.zfill(10)

    # rule: digits must not all be identical
    if len(set(s)) == 1:
        return False

    digits = [int(c) for c in s]
    # weights 10..2 applied to the first 9 digits
    rem = sum(d * w for d, w in zip(digits[:9], range(10, 1, -1))) % 11
    check = rem if rem < 2 else 11 - rem
    return check == digits[9]

Notes and small improvements:

  • zfill(10) clearly communicates the left-padding rule and avoids index tricks.

  • Do the "all-equal" test after padding so short inputs like "11111111" are not accidentally treated as valid patterns.

  • Keep the value as a string until the very end; converting to int too early would drop meaningful leading zeros.

  • For Python 2 compatibility, replace the final input call with raw_input. In Python 3, use input():

    if __name__ == '__main__':
        print(is_valid_id(input('Enter your ID Code Number: ')))

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.