Question about off-platform project

Hey everyone! So I am working on the off-platform project for the lesson on strings, lesson 7 in the Learn Python 3 course:
https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication

And there is an exercise where we have to code a message to change each letter by the letter which is 10 positions prior in the alphabet. While working on the exercise, it seems like the output letter changes when the input is a lowercase or an uppercase, the uppercase one being wrong. Could someone explain that?
I will write the code and the outputs below:

No uppercase:

alphabet = “abcdefghijklmnopqrstuvwxyz”
punctuation = “., ?! '”
message = “this is starting to get a bit more difficult, i am enjoying it though!”
translated_message = “”

for letter in message:
if not letter in punctuation:
letter_value = alphabet.find(letter)
translated_message += alphabet[(letter_value -10) % 26]
else:
translated_message += letter
print(translated_message)

output:
jxyi yi ijqhjydw je wuj q ryj cehu tyvvyskbj, y qc udzeoydw yj jxekwx!

With uppercase:

alphabet = “abcdefghijklmnopqrstuvwxyz”
punctuation = “., ?! '”
message = “This is starting to get a bit more difficult, I am enjoying it though!”
translated_message = “”

for letter in message:
if not letter in punctuation:
letter_value = alphabet.find(letter)
translated_message += alphabet[(letter_value -10) % 26]
else:
translated_message += letter
print(translated_message)

output:
pxyi yi ijqhjydw je wuj q ryj cehu tyvvyskbj, p qc udzeoydw yj jxekwx!

You can see the first letter changes, as well as the first one after the comma.
Is there an explanation to this? Does Python read a different value for the capital letter?

Thanks!

This is a quote from the definition of the find method on w3schools

The find() method finds the first occurrence of the specified value.

The find() method returns -1 if the value is not found.

The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found. (See example below)

Since your list, alphabet, has no capital letters, find returns -1 for any capital letter in the submitted string. If you look through the results, both T and I return a p. You could use letter.lower() in your program to account for unwanted capitalization.

Edit: Python considers capital and lower case letters completely different, I believe this is a general programming rule. Programs can be made to ignore the difference if that is what is desired. Here’s an example of how characters are seen by the computer: https://www.ascii-code.com/

1 Like

That is great! This was very helpful, thank you bavarcarus :slight_smile:

1 Like