Back to Freecodecamp

Step 24

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

latest1.3 KB
Original Source

--description--

It's time to test the encrypt function. Using the same arguments, replace your caesar call with a call to encrypt. You'll see the same output on the terminal.

--hints--

Your encrypted_text variable should have the value of encrypt('freeCodeCamp', 3).

js
({ test: () => assert(runPython(`_Node(_code).find_variable("encrypted_text").is_equivalent("encrypted_text = encrypt('freeCodeCamp', 3)")`)) })

--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'

    if not encrypt:
        shift = - shift
    
    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

def encrypt(text, shift):
    return caesar(text, shift)
    
def decrypt(text, shift):
    return caesar(text, shift, encrypt=False)

--fcc-editable-region--
encrypted_text = caesar('freeCodeCamp', 3)
--fcc-editable-region--
print(encrypted_text)