[Python] Is there a better way to calculate Chilean RUT verification digit?

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    For those who doesn't know, Chilean RUT is an ID for residents, similar to USCIS#, and is composed by 7-8 numbers followed by a verification digit (calculated by a type of MOD11 check).

    I made the next python code to get that number using python lists, but I need to ask if there is a better, newest, smarter or shorter way to obtain it.

    MOD11 algorithm used:

    1. ID number of 8 digits
    2. Reverse it
    3. Multiply each digit by 2,3,4,5,6,7,2,3 (i used a 10 length number to try in case of larger number)
    4. Sum the multiplied values and calculate the modulus(%) by 11
    5. The verification digit is 11 - modulus
    6. If digit is 10 show 'K', if it's 11 show '0'.

    CODE:

    # Function to calculate digit using python lists
    def DIG(RUT):
    RUTrev=list(reversed(RUT))
    CAD10=list(str(2345672345))
    calc = list(int(RUTrev) * int(CAD10) for i in range(len(RUT)))
    digit = 11-sum(calc)%11
    if digit==10:
    digit="K"
    elif digit==11:
    digit=0
    else:
    digit=digit
    return digit

    # Using the function to show the entire ID
    RUT=input("Insert RUT number:/n")
    print(f"RUT with digit is: {RUT}-{DIG(RUT)}")


    OUTPUT:

    Insert RUT number:
    12345678
    RUT with digit is: 12345678-5

    Continue reading...

Compartilhe esta Página