Learn String Manipulation by Building a Cipher - Step 42

Tell us what’s happening:

I have been trying to work through this question “Now, instead of printing ‘space!’, use the addition assignment operator to add the space (currently stored in char) to the current value of encrypted_text.”

But I am just not sure as to how to advance with this question. I have tried if char+= encrypted_text: with no print() in there and it told me I was getting close. The encrypted_text is already set to ’ ’ but it is saying the space is added in char. Thank you!

Your code so far


# User Editable Region

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

for char in text.lower():
    if char += encrypted_text:
    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/125.0.0.0 Safari/537.36 Edg/125.0.0.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 42

    if char == ' ':
        print('space!')

In the given code, when char is an empty space, we print out: space! to the screen.

What the instruction asked you to do is:

When char is an empty space, instead of printing out: space! to the screen, append the space (which now is store inside the char variable, because char == ' ', right?) into the end of the string stored inside the encrypted_text variable.

The instruction also suggested we can use the addition assignment operator (+=) to append the space into value of encrypted_text.

Addition assignment operator example:

dot = "."
sentence = "Hello, world"

sentence += dot

print(sentence) 
# now sentence is: "Hello, world."

So, replace

print('space!')

with the appropriated expression.

2 Likes

The instruction also suggested we can use the addition assignment operator (+= ) to append the space into value of encrypted_text .

This is what really did it for me here. Thank you!
Another question, does the indent matter after the if statement or can it be on the same vertical line as the if statement?

Yes, the indentation does matter.

The code indented after the if statement only run when the condition of the if statement is true.

The code on the same indentation level as the if statement will always run no matter what the condition of the if statement is true or false.

1 Like

Sweet, thank you for the clarification and help on this question! I was searching for HOURS to figure this out

1 Like