Back to Freecodecamp

Step 20

curriculum/challenges/english/blocks/workshop-caesar-cipher/6819ca464db219ab86f37dc4.md

latest1.7 KB
Original Source

--description--

As you've already verified, the shift passed to encrypt the text should be positive. It cannot exceed 25 (the last index of alphabet) though.

Add a second condition to the if statement that verifies that shift is greater than 25. Remember that the logical OR operation in Python is implemented through the or operator.

Also, update the returned message to Shift must be an integer between 1 and 25.

--hints--

You should use the or operator to add a second condition after shift < 1 checking if shift is greater than 25. You should not modify the existing condition.

js
({ test: () => runPython(`
_cond = _Node(_code).find_function("caesar").find_ifs()[1].find_conditions()[0]
_options = ["shift > 25", "25 < shift", "shift >= 26", "26 =< shift"]
assert any(_cond.is_equivalent(f"shift < 1 or {option}") for option in _options)
`) })

Your second if statement should return Shift must be an integer between 1 and 25.

js
({ test: () => assert(runPython(`_Node(_code).find_function("caesar").find_ifs()[1].find_bodies()[0].has_return("'Shift must be an integer between 1 and 25.'")`)) })

--seed--

--seed-contents--

py
def caesar(text, shift):

    if not isinstance(shift, int):
        return 'Shift must be an integer value.'
    
--fcc-editable-region--
    if shift < 1:
        return 'Shift must be a positive integer.'
--fcc-editable-region--

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

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