Implement the Luhn Algorithm - Implement the Luhn Algorithm

Tell us what’s happening:

Hello,

I am trying to understand why the last 2 tests keep failling, while the example tests are correct. Could you please tell me what the tests entail so that I can debug the function? I hope you understand that I’m not going to put my credit card number in the code just to check if other cards outside the test ones work or not.

Kind regards,

Your code so far

def verify_card_number(card_number):
    
    card_number = ''.join(e for e in card_number if e.isalnum())
    
    check = int(card_number[-1])
    card_number = card_number[:-1]
    check_sum = 0
    for i in range(len(card_number),0,-1):
        if (len(card_number)-i) % 2 == 0:
            x = 2*int(card_number[i-1])
            if x >= 10:
                x -= 9
        else:
            x = int(card_number[i])
        check_sum += x
        #print(check_sum)
    
    check_sum += check
    #print(check_sum)
    valid = check_sum % 10 == 0
    #print(valid)
    if valid:
        return 'VALID!'
    else:
        return 'INVALID!'


verify_card_number('4111-1111-4555-11412')


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Implement the Luhn Algorithm - Implement the Luhn Algorithm

Welcome to the forum @jomles

In the example, the checksum for the number 453914881 should equal 42.

Your code produces the number 52.

Happy coding