Learn String Manipulation by Building a Cipher - Step 42

Tell us what’s happening:

Error is “Your code contains invalid syntax and/or invalid indentation.”

Your code so far

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)

Hello @pskidd
Python is very particular about indentation to run. I do not know if the posting process stripped the indentation or if this is the way you had the actual code. Either way, I made an indication in your code as to what needs the indentation and the level of it. Also there’s an extraneous closing square bracket that you must deal with it.

1 Like

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 (').

Many thanks to all. This is a wonderful forum!! it was just that extraneuous square bracket t the start that threw me. If there is a good online/installable python editor avaialable that will highlight these syntax errors please let me know!!! (PS I use a lot use both Chromebooks and Windows PCs)!!

Pete

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.