Learn String Manipulation by Building a Cipher - Step 18

Tell us what’s happening:

Why does my Caesar cipher code return -1 for uppercase letters in Python?

I’m trying to implement a simple Caesar cipher in Python. I want to shift each letter in a string by 3 positions using the English alphabet. Here’s the code I have so far:

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

index = alphabet.find(text[0])

When I run this, I get -1 for the first character because it’s uppercase (‘H’). I learned that .find() is case-sensitive, so I tried using .lower()

Your code so far


# User Editable Region

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(text.lower())
print(alphabet.find(text[0].lower()))

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

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 18

Welcome to the forum @Yanniss

Here is a comparison of the original code and your code.

The code in blue is the original code, the code in red is your code.
The code in magenta is the overlap.

Remove the last print() call.
You should not have print(text.lower()) in your code.

It looks like you changed the value of the index variable.

Also, you need to remove a print call.

Please reset the step to restore the seed code and try again.

Happy coding