Caesar Cypher Decodes Correctly But Doesn't Code Correctly in Python - Several of the Letters are Appearing as "Z"

Viewed 22

I have a homework assignment to create a Caesar cypher code that offsets by 10 to reveal a hidden message. I was able to decode the message, but when I try to turn a message into a coded message it goes wonky. When I run my coded message through the decoder it comes back with "z" in the places of several different letters. Here is what I have from start to end.

#Decode Vishal's message using an offset of 10

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
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!"
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)

#prints
#hey there! this is an example of a caesar cipher. were you able to decode it? i hope so! send me a message back with the same offset!



#Send Vishal a coded message back

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
message = 'Hey Vishal! This is a very cool cipher and I love the history lesson too. Thank you for sharing this with me.'
coded_message = ''

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

#prints
#puo pyixqb! pxyi yi q luho seeb syfxuh qdt p belu jxu xyijeho buiied jee. pxqda oek veh ixqhydw jxyi myjx cu.

#When I run this code back through the decoder I get this
#zey zishal! zhis is a very cool cipher and z love the history lesson too. zhank you for sharing this with me.

Can you tell me why this is happening? Thank you in advance. :~)

0 Answers
Related