Learn String Manipulation by Building a Cipher - Step 41

Tell us what’s happening:

I did a few trials and I lost it pls explain it to me in the simplest fourm. thanks!

Your code so far


# User Editable Region

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

for char in text.lower():
    if True 
    if (char)'space!' 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/127.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 41

Hello,
if statements evaluate to true if the condition they are examining is True, if it is not they evaluate to False
Consider this example, write an if statement that evaluates to True if the age variable is greater than 18 and prints adult, if it evaluates to False , it will not do anything

age = 20
if age > 18 :
   print('adult')

This is very similar to your situation except you are checking if the variable char is empty space and printing space if it is
Hope this helps and if you are still confused check this out

1 Like

Hi @codybryant

Here is a comparison of the original code and your code.

The code in blue is the original code, the code in red is your code.
The code in magenta is the overlap.

image

You should replace print(char == ' ') with an if statement that triggers when char == ' ' . Do not use parentheses to enclose the if condition and remember to include the final colon.

  1. remove the if True condition
  2. use the hint message to structure the if condition. Do not use round braces. Remove False
  3. the print statement needs indentation

Happy coding

1 Like

Look at the example code again and use the exact same format:

if x != 0:
    print(x)

You can copy/paste this and replace the example variables with your own.

If char is equal to ' ' then print 'space!'

1 Like