Learn String Manipulation by Building a Cipher - Step 48

Tell us what’s happening:

I am unable to pass this help me!
text = ‘Hello Zaira’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
encrypted_text = ‘’

for char in text.lower():
if char == ’ ':
encrypted_text += char
else:
index = alphabet.find(char)
new_index = (index + shift) % len(alphabet)
encrypted_text += alphabet[new_index]

print(‘Plain text:’, text)
print(‘Encrypted text:’, encrypted_text)

Your code so far


# User Editable Region

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

for char in text.lower():
    if char == ' ':
        encrypted_text += char
    else:
        index = alphabet.find(char)
        new_index = (index + shift) % len(alphabet)
        encrypted_text += alphabet[new_index]

print('Plain text:', text)
print('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/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 48

Welcome to the forum @naoll

Check the casing of the string.

You should print 'plain text:', text after your for loop.

Happy coding

still am having trouble .

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

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

Please talk about how you are stuck.


You need to use exactly what the instructions asked for, with exactly the same spaces and capitalization.

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

Hi Jeremy,

I’ve been working on the Caesar cipher, but I’m still unable to pass the test on FreeCodeCamp. My code is below, and I’ve checked the output carefully to make sure there’s no extra spacing. However, it still isn’t passing.

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

for char in text:
    if char == ' ':
        encrypted_text += char
    else:
        index = alphabet.find(char.lower())
        new_index = (index + shift) % len(alphabet)
        new_char = alphabet[new_index]
        
        if char.isupper():
            encrypted_text += new_char.upper()
        else:
            encrypted_text += new_char

print('Plain text:', text)
print('Encrypted text:', encrypted_text)

out put:  Plain text: Hello Zaira
                 Encrypted text: Khoor Cdlud

You should read what I wrote here.

1 Like

'plain text:', text <=== This is the expected code
'Plain text:', text <=== Here is your code
^