Learn String Manipulation by Building a Cipher - Step 42

Tell us what’s happening:

What am I doing wrong here exactly?
I tried a plethora of different things but the code either didn’t run at all or it didn’t pass.
I’d appreciate any help,

Your code so far


# User Editable Region

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

for char in text.lower():
     if char == ' ':
      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/121.0.0.0 Safari/537.36 OPR/107.0.0.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 42

You have indentation issues. Notice that index should be align with if.

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

I tried this, still didn’t work

Remove one blank space in front of the if.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

You need to indent each code block. It’s the only way Python can tell which code is part of the for or if blocks.

code
code

for this in that:
    code
    code
    if this == that:
        code
        code
    code
    code

code
code
1 Like

an other issue is that you need to put quotes around space!