Build a Caesar Cipher - Step 22

Tell us what’s happening:

Why is step 22 not checking off? I am confused what to do to complete the task, I have tried many methods already.

Your code so far

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

    if encrypt == False:   
         shift = - shift

    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'
    
# User Editable Region
    
# User 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)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0

Challenge Information:

Build a Caesar Cipher - Step 22

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-caesar-cipher/681a1d60e037127e7c8e0230.md at main · freeCodeCamp/freeCodeCamp · GitHub

Create an if statement that checks if encrypt is not truthy.

“not truthy” could have other values than equal to False. It could be null or 0.

https://www.geeksforgeeks.org/python/truthy-vs-falsy-values-in-python/

I tried changing the if statement to ‘‘if not encrypt:’’ then under it put the same shift = - shift but this still doesn’t work and sends me a message saying “You should set shift to - shift when encrypt is not truthy.”

Can you please share you code?

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

    if not encrypt:
        shift = - shift
    

    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'

consider if these should happen before or after shift = -shift

for example, if shift is a string, what happens on the shift = -shift line?

or also, if shift is 3, then, shift = -shift turns it into -3 what happens on the line if shift < 1 or shift > 25:?

Thanks for your help, I ended up moving the if statement and it solved my problem.