Luhn algorithm step 5

Tell us what’s happening:

No matter how I try to print the translated_card_number to the console it tells me it is incorrect. I’ve listed all the ways I’ve attempted to do the task in the code.
The only instructions are to print the translated_card_number to the console.
The hint tells me I should print the translated card number to the console…
What is the ‘correct’ way that it wants me to do this…?

Your code so far


/* User Editable Region */

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

/* User Editable Region */

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 5

Have you tried to do it inside the function?

1 Like

If you look closer, it is: inside the function, printed by calling the function in the print command, and in a separate print command outside of the function. I’ve tried all three separately and together.

After fiddling with it, it wants it like this.

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

    print(translated_card_number)

Not sure why the spacing mattered there.

If you mean the empty line before the print() call, it doesn’t matter. For me, the only thing was preventing you to pass the test was the last print(translated_card_number) outside the function, because the variable is not defined there.

Right, I knew that one wouldn’t work because the variable wasn’t defined outside of the function. But the only instructions said “Print translated_card_number to the console” so I figured the intended solution might not have been indented correctly. Hence why I included the one outside of the function.
And to reiterate I tried each of the three methods to print by themselves before I combined them all for this post.

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