Implement the Luhn Algorithm - Implement the Luhn Algorithm

Tell us what’s happening:

I’m having trouble understanding how running the string ‘4111-1111-1111-1111’ through this algorithm is supposed to return VALID as the output, since the resulting sum of 4+2+1+2+1+2+1+2+1+2+1+1 (with the last digit not being changed due to being a check digit) is not a multiple of 10. I’ve also noticed a significant difference in previous labs compared to current labs when looking for answers on other projects, have the courses been updated since feb 2026?

Your code so far

def verify_card_number(digits:str):
    if '-' in digits:
        digits = ''.join(digits.split('-'))
    if ' ' in digits:
        digits = ''.join(digits.split(' '))
    print(digits)
    dig_as_num = [int(num) for num in digits]
    print(dig_as_num)
    for i in range(1, len(dig_as_num)):
        if i % 2 == 1 and i != len(dig_as_num) - 1:
            dig_as_num[i] *= 2
        if dig_as_num[i] > 9:
            dig_as_num[i] = int(str(dig_as_num[i])[0]) + int(str(dig_as_num[i])[1])
    print(dig_as_num)
    
    total = 0

    for i in dig_as_num:
        total += i
    
    print(total)
    if total % 10 != 0:
        return "INVALID!"
    return "VALID!"

    print(dig_as_num)


print(verify_card_number('4111-1111-1111-1111'))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0

Challenge Information:

Implement the Luhn Algorithm - Implement the Luhn Algorithm

you are doubling the wrong digits, you need to double the one immediately next to the check digit, as it says here:

  • Starting from the right, and excluding the rightmost digit (the check digit), double the value of every other digit.

so you also double the 4

The example given shows the doubling starts with what would be index 1 on a zero index list

I seem to have misread, I was doubling from left to right. Apologies

you are comparing an odd number of digits and an even number of digits, you need to always double the digit that is next to the check digit, and then skip digits following that