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

Tell us what’s happening:

When I am doing it for the even numbers do i have to make the int value different so it does not mess up with the previous code?

When I enter the new code in for even I pretty much copied the code from the odd digits but made it even. Now I am getting an error telling me that I need to have a print(digit) within my for loop, but I feel like I do.

Your code so far

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

    for digit in odd_digits:
        sum_of_odd_digits += int(digit)
    print(sum_of_odd_digits)


# User Editable Region

    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    
    for digit in even_digits:
        sum_of_even_digits += int(digit)
    print(digit)


# 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge Information:

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

Loop over the even digits and print each to the console.

You should do exactly what the instructions says. This line shouldn’t be inside your loop.

Also, print(digit) is not inside the loop. You need to indent it.

Thank you, I think i’m overthinking the questions sometimes and trying to add too much cuz that was simple.

Also what would you call it when the print function is not indented, and why does that create the error?

Python relies on indentation. So if you write something like:

for i in lst:
print(i)

An IndentationError is raised because Python expects an indented block after the colon. In your case, having

does not raise an indentation error, because you can still access the value of digit at the last iteration after the loop. But it’s not what you have been asked to.

I’m not sure if this answer to your question or you wanted to know something else.

Yes this does answer my question, let me check that I understand it though.

In my example it doesn’t give the error because it is still pulling the information from the line right above it?

The second line is properly indented and perform a valid operation. So the loop does not throw an error.

The following print can access the value of digit (at the last iteration because it is after the loop) and so no error.

If you have doubts about where you can access variables, I suggest to make a brief research and read something on the variable scope in Python.

This might have too many details, but if you are interested you’ll find many useful things there.