For some odd reason, I have a syntax error ‘return’ in my code. I have written what it has asked for, but for some peculiar reason, it has some sort of syntax error. I have added the “valid syntax,” meaning I have added the right lining(indentation) and colons. But I can’t help to find what the problem is exactly.
Your code so far
# User Editable Region
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)
# User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Challenge Information:
Learn String Manipulation by Building a Cipher - Step 41
At the top of your for loop, replace print(char == ' ') with an if statement. The condition of this if statement should evaluate to True if char is an empty space and False otherwise. Inside the if body, print the string 'space!' . Remember to indent this line.
You are not asked to return True or False.
You are also not asked to add an else statement.
Please reset the step to restore the seed code and try again.
Thanks for the reply! I have alter my code to if char != ‘’:
print(‘space!’). Removing both the return True, False, and else statement, but it is still saying that the code does not pass.
What made you write return? It’s not mentioned in the instructions and as the error states, you cannot use it outside of a function, and functions have not yet been introduced.
replace print(char == ' ') with an if statement
Please note:
' ' #space character
'' #empty string
You need a space between the quotes to represent a space character.
The condition of this if statement should evaluate to True if char is an empty space and False otherwise
if a == "24":
This part is a conditional expression a == "24". When we say it evaluates to true or false, this is not something that you need to code, it happens by itself. If the a variable has the value 24 then you can imagine this expression being replaced with true
if true:
And it will execute the code nested in the if. if a has a different value, 45 for example it will evaluate to false, and you can imagine it being replaced with false
if false:
and it will skip over that code. Again this is not something you need to code yourself, these evaluations are how Python works. You can consider these equivalent, they could replace each other: (when a has the value 24) a == 24, 24 == 24, true