Coded Correspondence - Not outputting correct decoded message

Hi! I’m currently working on the Off-Platform Project: Coded Correspondence. I successfully decoded the first message and successfully figured out how to encode a message.

Encoding a message

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
message = "Hi!  Good luck on this project!"
offset = 10
encoded_message = ""

for letter in message:
    if not letter in punctuation:
        index = alphabet.find(letter)
        encoded_message += alphabet[(index - offset) % 26]
    else:
        encoded_message += letter

print(encoded_message)
    

Output py! peet bksa ed jxyi fhezusj!

Decoding the message

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
#original_message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"
message = "py!  peet bksa ed jxyi fhezusj!"
offset = 10
decoded_message = ""

for letter in message:
    if not letter in punctuation:
        index = alphabet.find(letter)
        decoded_message += alphabet[(index + offset) % 26]
    else:
        decoded_message += letter

print(decoded_message)

Output zi! zood luck on this project!

When I first created my message I used capital letters and it would not output a correct decoded message. I’ve since figured out that if I write my message with lower case it will encode and decode correctly. My question is why did the capital letters make the difference?

As a follow up, what would be the best way for me to find an answer to this myself? I’m trying to be as self sufficient as possible but I am also trying to lean into the community to be more social and work with other people who are learning.

There are no capital letters in the alphabet string,
so when it does alphabet.find( )
the gives a result of -1 since that letter was not found in the string.
Note that alphabet[-1] is 'z' because that’s the last thing in the string alphabet.
And that became 'p' after the offset encoding
since -1 - 10 = -11
and (−11) % 26 = 15
alphabet[15] is 'p'

I guess you could keep the capitalization by changing alphabet to have the capitals too.
One way is to add capitals to alphabet by doing:
alphabet = alphabet + alphabet.upper()

Your code, with minor changes:

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
punctuation = ".,?'! "
message = "Hi!  Good luck on this project!"
offset = 10
encoded_message = ""

for letter in message:
    if letter not in punctuation:
        index = alphabet.find(letter)
        if index < 26:  # use lowercase
          encoded_message += alphabet[(index - offset) % 26]
        else:  # use uppecase
          encoded_message += alphabet[((index - offset) % 26) + 26]
    else:
        encoded_message += letter

print(encoded_message)

# now doing decoding:
message = encoded_message  # message is encoded message
decoded_message = ""

for letter in message:
    if letter not in punctuation:
        index = alphabet.find(letter)
        if index < 26:  # use lowercase
          decoded_message += alphabet[(index + offset) % 26]
        else:  # use uppercase
          decoded_message += alphabet[((index + offset) % 26) + 26]
    else:
        decoded_message += letter

print(decoded_message)
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" punctuation = ".,?'! " message = "Hi! Good luck on this project!" offset = 10 encoded_message = "" for letter in message: if letter not in punctuation: index = alphabet.find(letter) if index < 26: # use lowercase encoded_message += alphabet[(index - offset) % 26] else: # use uppercase encoded_message += alphabet[((index - offset) % 26) + 26] else: encoded_message += letter print(encoded_message) #original_message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!" message = encoded_message # message is encoded message decoded_message = "" for letter in message: if letter not in punctuation: index = alphabet.find(letter) if index < 26: # use lowercase decoded_message += alphabet[(index + offset) % 26] else: # use uppercase decoded_message += alphabet[((index + offset) % 26) + 26] else: decoded_message += letter print(decoded_message)
3 Likes