Python Caesars Cypher

Hey!! so i tried my best with the Caesars Cypher but I am completely lost as to how to fix this error. If anyone have any suggestions please help!!

Thank You in advance

P.S : I am not sure if this is the right way to code this but it worked for string “hello” when i tested.

import string alphabet = list(string.ascii_lowercase) alphabet.append(" ") alphabet.append("!") alphabet.append(".") alphabet.append("?") message_to_decode = "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!" def encode(word, s): new_word = [] text = [] for i in word: char = alphabet.index(i) text.append(char) for i in text: new_word.append(alphabet[i - s]) str_word = "".join(new_word) return str_word def decode(word, s): new_word = [] text = [] for i in word: char = alphabet.index(i) text.append(char) for i in text: new_word.append(alphabet[i + s]) str_word = "".join(new_word) return str_word # encode("hello", 3) decode(message_to_decode, 10)

You may want to change the thing you’re using for the index, i - s , to (i - s) % 26 to ensure that what you get is something in 0 to 25 (since alphabet had a length of 26)
Similarly, you may want to change i + s to (i + s) % 26

Also, you encode/decode every letter, but you may want to put an if statement in your loops so that you skip characters that are not in alphabet.

1 Like

How’d you end u going with this? As I’m currently stuck