Learn String Manipulation by Building a Cipher - Step 49

Tell us what’s happening:

Need help checking my code:
text = ‘Hello Zaira’
shift = 3
def caesar():
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
def caesar():
    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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 49

1 Like

Welcome to the forum :wave:

indent all the following lines

Every single line after the def should be indented, all the way to the end of the code

Welcome to the community! you’re very close. Here are few hints which can help you out.

  1. The else condition inside the function seems misplaced. Think in terms of whether the indentation is correct after the if statement.

  2. Also encrypted_text is defined inside the function and then being called outside the function. Does that seem correct? Think about what might be missing at the end of the function so that the variable encrypted_text can be used outside the function.

Good luck! Let us know if you need any more help!

This worked, thanks:
removed by mod

1 Like

Nice work! Glad we could help!

Glad you got it :+1:

Please don’t post solution code to the forum