Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 17

Tell us what’s happening:

I was informed by the system that I should have odd_digits = card_number_reversed [ : : 2] to be able to proceed. However, I could not properly understand why that is, I mean, I could not get the point of having every other digit within the card_number_reversed - what is that for?

Also, why should 2 digits be skipped, as we can see in the command?

``````
# User Editable Region

def verify_card_number(card_number):
sum_of_odd_digits = 0
card_number_reversed = card_number[::-1]
odd_digits = card_number_reversed

# User Editable Region

def main():
card_number = '4111-1111-4555-1142'
card_translation = str.maketrans({'-': '', ' ': ''})
translated_card_number = card_number.translate(card_translation)

verify_card_number(translated_card_number)

main()
``````

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

Challenge Information:

Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 17

I suggest you to take another look to step 9 or read something else about how the Luhn algorithm works:

The Luhn algorithm is as follows:
From the right to left, double the value of every second digit; if the product is greater than 9, sum the digits of the products.
Take the sum of all the digits.
If the sum of all the digits is a multiple of 10, then the number is valid; else it is not valid.

You need to retrieve every other digit so you’ll be able to double them and compute their sum. Does that answer to your question?

1 Like

Thank you so much! I will check again the Luhn algorithm concept to better understand it. This helped me a lot. Thank you again!

1 Like