Learn String Manipulation by Building a Cipher - Step 42

Tell us what’s happening:

i’m not sure that i understand the instructions. I have tried few possible coding for addition operator. however, it seems i got it wrong all the time. I might need some explanation on the instructions, probably different way of explaining the question because i am a bit lost in understanding it. Thanks for the 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('')
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text += char
    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.

Example:

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

sentence += dot

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

i still could not understand it. If we get rid the 'print(‘space!’), it will produce error since it has to be indented block after the ‘if’ function. Whereas to append the space into encrypted_text, currently the value is added with alphabet[new_index]. I have tried to replace the value with the space it shows error as well. i still can’t get it.

text = ‘Hello World’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
encrypted_text = ’ ’

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

Which value of ‘encrypted_text’ variable should we append the space because there one before the ‘for’ function on another one is inside the function? what should we do with the ‘if’ function for char == ’ '? should we remove it?

Welcome back to the forum @dinArnorld88

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 .

The original code.

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)

The new expression needs to replace the old code.

Old code:

        print('space!')

The new code will occupy the same line as the old code.

Happy coding

@dinArnorld88
We append the space into encrypt_text only when char == ' '.
So “the expression appending the space into encrypt_text” has to be indented inside the if statement.

What you need to do now is replace

        print('space!')

with the expression appending space into encrypted_text, with the same indentation level.

Side note, you need to use the Preformatted Text button when posting code on the forum to make it preformatted:

now i got it right at last. thanks for the clarification. I will use the Preformatted text next time when showing my coding works in the middle

1 Like