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

Tell us what’s happening:

Hi, I am on step 19 of the Luhn Algorithm and I am trying to create a for loop to iterate over each digit in the odd_digits variable. I believe I am missing something in my code, because it displays an error each time I check my code. I am currently trying
for odd_digits in card_number:
print(card_number)

Your code so far


# User Editable Region

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

# 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()

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

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

reset the step, you should not remove the lines that were there before, you have removed the delcaration of the odd_digits variable

then, you need to use a for loop as instructed

Use a for loop to iterate over each digit in the odd_digits list.

2 Likes

Let’s read the request again.

Use a for loop to iterate over each digit in the odd_digits list. Move your print call from the previous step into the for loop, and change it to print each digit.

Notice the words for loop, digit and odd_digits. Also, notice the last three words print each digit. There’s digit again associated with each (iteration)

You wrote

Which loops over card_number (instead of odd_digits) and each time it sets odd_digits (instead of digit) and then you print card_number (instead of digit)

In Python, the thing that a for loops with, is called iterator (something that it can be iterate over, like a list or collection). Thus it gets usually named in plural, to denote multiple items.
In the example you posted, card_number should be card_numbers if it were correct.
A convention in Python when you iterate over a list of items is to also name the current item in single form from the list, in this case would be card or card_number. If we were to place everything together, the example would show as

for card_number in card_numbers:
     print(card_number)
1 Like

I was able to figure it out. I needed to use
for digit in odd_digits:
print(digit)

I didn’t think to use digit before, but after reading these suggestions I made it work. Thanks all!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.