Implement the Luhn Algorithm - Implement the Luhn Algorithm

Tell us what’s happening:

Hi. I just wanted to mention that the instruction says “double the value of every other digit.”
But in the provided example, it seems -in the table- that only the even positions are doubled.

Your code so far

def verify_card_number(card):
    card=card.replace('-', '').replace(' ', '')
    v=int(card[len(card)-1])
    print('init', v)
    for i in range(len(card)-1):
        r = int(card[i])*2
        if r > 9:
            r-= 9
        print(card[i], r)
        v+= r
    return 'INVALID!' if v%10 else 'VALID!'

print(verify_card_number('453914889'))

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36

Challenge Information:

Implement the Luhn Algorithm - Implement the Luhn Algorithm

if there is a different number of digits, then it can also be the odd ones, you need to not double the check digit (the last one) but the one immediately to its left

1 Like

Thank you for the reply.

The first step in the lesson states:

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

This suggests:

Account number 4 5 3 9 1

Double every other 8 10 6 18 1

I suggest that the instruction replaces “every digit” with “every second digit”

As in:
[… ] Start with the payload digits. Moving from right to left, double every second digit, starting from the last digit. […] [From Wikipedia]

it says “every other digit” which means the same thing

but if you want to propose the change please open a github issue

1 Like