I am currently making a cryptography file, it's supposed to take a user key and a user input, and work through it like a caesar cipher except it multiplies the key by 2 after each letter. After testing it, i ran into this error:
line 40, in <module>
encode("p", 16) line 15, in encode
if decoded_message[place_in_message] == letter:
IndexError: string index out of range`
here's the code that I was using:
import array as arr
alphabet = arr.array('u', ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"])
decoded_message = input("What's your message? ")
amount_of_characters = (len(decoded_message))
key = int(input("What's your key? "))
place_in_message = 0
encoded_message = ""
_ = 0
def encode(letter, letterNum):
global key
global place_in_message
global alphabet
global decoded_message
global encoded_message
if decoded_message[place_in_message] == letter:
if key + letterNum < 26:
encoded_message = encoded_message + alphabet[letterNum + key]
place_in_message = place_in_message + 1
key = key * 2
else:
key = key - 26
encoded_message = encoded_message + alphabet[letterNum + key]
place_in_message = place_in_message + 1
for _ in range(amount_of_characters):
encode("a", 1)
encode("b", 2)
encode("c", 3)
encode("d", 4)
encode("e", 5)
encode("f", 6)
encode("g", 7)
encode("h", 8)
encode("i", 9)
encode("j", 10)
encode("k", 11)
encode("l", 12)
encode("m", 13)
encode("n", 14)
encode("o", 15)
encode("p", 16)
encode("q", 17)
encode("r", 18)
encode("s", 19)
encode("t", 20)
encode("u", 21)
encode("v", 22)
encode("w", 23)
encode("x", 24)
encode("y", 25)
encode("z", 26)
print(encrypted_message)
I'm not sure why this error is happening, I looked through my code and didn't see anything wrong. Thanks for helping
globalkeyword in Python.