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

Tell us what’s happening:

I find myself getting the output I’m supposed to be getting but it says my code doesn’t pass again.

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)


/* User Editable Region */

    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    for digit in even_digits:
        number = int(digit) * 2
        if number >= 10:
            number = (number // 10) + (number % 10)
        sum_of_even_digits += number
        total = sum_of_even_digits + sum_of_odd_digits
        print(total)

/* 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/120.0.0.0 Safari/537.36

Challenge Information:

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

@generic_0 have you tried changing the indentation of total and print? I tink it should be more to the left

Yeah i tried doing that, still not passing

i think the mistake is that you wrote even digits+ odd digits and not odd +even

and the check the indentation

I thought of it earlier, I tried doing that too - didnt make a difference

I checked this too, the total and print statements are outside the if statement
and inside the for loop

and you wrote sum_of_odd_digits + sum_of_even_digits in the right order?

yes, total = sum_of_odd_digits + sum_of_even_digits

can you share your code again?

1 Like

It should be outside second for loop not inside second loop but inside function.

also total = sum_of_odd_digits + sum_of_even_digits

Got it, This worked - when it said below the for loop, i thought inside it. Thanks again for the help.

Another question - how do I send the code in the correct format while replying to the main message.

don’t send final answer. The moderators are trying to ensure solutions are not displayed through forum otherwise other students will copy and paste and doesn’t think on how to solve.

I was trying to send the updated code which was still not working before but i couldn’t send it in the correct format because it was taking it as normal text. So i wanted to know how I can send it in code format. It’s only for future references

</> use this preformatted text for it when u reply

1 Like

In my case when this happens I just RESET :repeat: the Step and rewrite it manually, it seems that the code box sometimes does not accept copy and paste code, I’m working with VS code hence the copy paste is a must to transpolate the code from VS to FreeCodeCamp desktop app.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read. (Backtick shares a button with Tilde ~)

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').