Back to Freecodecamp

Step 10

curriculum/challenges/english/blocks/workshop-caesar-cipher/6810891bd6de8a87a833df42.md

latest1.6 KB
Original Source

--description--

As you can see from the output, the message has been encrypted. The next step will be making your code reusable in case you wanted to encrypt different messages.

For that, you'll need to create a function. As a reminder, here's how to create a function named spam that prints Spam! on the terminal:

py
def spam():
    print('Spam!')

Create a function named caesar. Put all your existing code within the function body. Pay attention to keep the same indentation level for all of the lines within the function body.

--hints--

You should have a function named caesar.

js
({ test: () => assert(runPython(`_Node(_code).has_function("caesar")`)) })

You should move all the code you wrote so far within the caesar function body. Make sure you keep the same indentation level for all of the lines within the function body...

js
({ test: () => assert(runPython(`_Node(_code).find_function("caesar").find_body().is_equivalent("alphabet = 'abcdefghijklmnopqrstuvwxyz'\\nshift = 5\\nshifted_alphabet = alphabet[shift:] + alphabet[:shift]\\ntranslation_table = str.maketrans(alphabet, shifted_alphabet)\\ntext = 'hello world'\\nencrypted_text = text.translate(translation_table)\\nprint(encrypted_text)")`)) })

--seed--

--seed-contents--

py
--fcc-editable-region--

alphabet = 'abcdefghijklmnopqrstuvwxyz'
shift = 5
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
translation_table = str.maketrans(alphabet, shifted_alphabet)
text = 'hello world'
encrypted_text = text.translate(translation_table)
print(encrypted_text)
--fcc-editable-region--