Back to Freecodecamp

Step 22

curriculum/challenges/english/blocks/workshop-caesar-cipher/681a1d60e037127e7c8e0230.md

latest1.3 KB
Original Source

--description--

You are going to use the parameter added in the previous step to determine if the function should encrypt the text passed to it (default behavior, encrypt=True), or if it should decrypt an encrypted message.

Create an if statement that checks if encrypt is not truthy. Within the new if statement, set shift to - shift. This is necessary to enable the shift to take place in the opposite direction with respect to the encryption process.

--hints--

You should set shift to - shift when encrypt is not truthy.

js
({ test: () => assert(runPython(`caesar('Ymnx Nx F Yjxy', 5, False) == 'This Is A Test'`)) })

--seed--

--seed-contents--

py
def caesar(text, shift, encrypt=True):

    if not isinstance(shift, int):
        return 'Shift must be an integer value.'

    if shift < 1 or shift > 25:
        return 'Shift must be an integer between 1 and 25.'

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

encrypted_text = caesar('freeCodeCamp', 3)
print(encrypted_text)