Shifting in a string. Coded message visual studio

Hi everyone,

I am trying to work on the coded correspondance project. So the idea is to create a code that shifts back ten letters of the alphabet. I have the exercise correct but if I write the message in uppercase the outcome is always de letter “p”

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = " ,!"
message = "TEXT me, to my office number, as soon as you finish the project!"
coded_message = ""
for letter in message:
    if not letter in punctuation:
        letter_value = alphabet.find(letter)
        coded_message += alphabet[(letter_value - 10) % 26]
        coded_message += letter

The output will be:

pppp cu, je co evvysu dkcruh, qi ieed qi oek vydyix jxu fhezusj!

Why is this?

Thank you!

alphabet is a string of only lower case letters.
So, alphabet.find(letter) wont be able to find anything and so instead return -1 to letter_value.
The next line will then have it looking for the index at
[ ( -1 - 10) % 26 ] = [ -11 % 26 ] = [15]
which is… p.

Hope that helps

1 Like