Learn String Manipulation by Building a Cipher - Step 42

Tell us what’s happening:

what have i done wrong
text = ‘Hello World’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
encrypted_text = ‘’

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

Your code so far


# User Editable Region

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

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

# 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/124.0.0.0 Safari/537.36 Edg/124.0.0.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 42

print the result of comparing char with a space (' ')

A space string, a space character in quotes. Not the word “space” but " "

In a sentence, if you are going through it character by character, you will encounter spaces. “I”, “n”, " ", “a”, " ", “s”, “e”

Don’t use if just print the result of the comparison

still wrong do you have any more ideas but thanks
text = ‘Hello World’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
encrypted_text = ‘’

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

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

You’re still comparing char to the word space, not a space character

"space" #the word space
' '     #space character

And you are printing the word “space”, not the comparison (the expression with == is a comparison because it compares the left and right sides of the == and returns true if they are equal and false if they are not).

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

for char in text.lower():
if char == " “:
print(”==")
encrypted_text == char
else:
index = alphabet.find(char)
new_index = index + shift
encrypted_text += alphabet[new_index]
print(‘char:’, char, ‘encrypted text:’, encrypted_text)
thanks tho this is my first time trying coding

1 Like

Can you try formatting this as code so the indentation is visible?

char == " "

This is a comparison, comparing char and " "

"=="

This is a comparison operator

No need for if because the instructions don’t mention it

1 Like

under that i put
print(“false”)
what should it be

The loop will cycle through each character in the sentence. You don’t know if it will be a letter or a space.

Instead of printing “False”, just print the comparison:

char == " "

This is the comparison

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