Hey, new to Python! Stuck on an SyntaxError: 'return' on my code on Step 41 on FreeCodeCamp.

Hey everyone, new to Reddit here! I'm learning Python on FreeCodeCamp, while also learning Python on CS50P as well. Anyways, on my terminal it says "Traceback (most recent call last): File "main-py", line 8 SyntaxError: 'return' outside function", but I'm unsure what could be causing a traceback within my code. It seems like anywhere within the if statement, typing return True has a Syntax error anyway. Unsure if I should avoid adding return, but still would need to return a True or False value anyway.

The lines of code that I have added for step 41(problem set) were the if statement through the print('space!'). It does mention print(char == ' '), which I have removed and implemented for the if loop. The if and else statement has the colons attached. Indentation doesn't seem to be off. The if and else statements are aligned. If anyone could help, I would be greatly appreciate.

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''

for char in text.lower():
    if char == '':
        return True
    else:
        return False
        print('space!')
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text += alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)

Posted Aug 14, 2025, 12:38 AM UTC

620
Loading comments…