0

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

2
  • Don't use _ as an actual variable = 0 Commented Jan 30, 2019 at 17:11
  • Variables that are only referenced inside a function are implicitly global. You don't need to mark them as global in the function, you can still access them if you just remove those lines. You almost never need to use the global keyword in Python. Commented Jan 30, 2019 at 17:20

1 Answer 1

1

I'm going to put a lot of effort into this answer because I'm bored, so first, let's clean up your code. (The code below works)

class MessageEncoder:
    def __init__(self, decoded_message, key):
        self.decoded_message, self.key = decoded_message, key

        self.alphabet = ["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"]
        self.place_in_message = 0
        self.encoded_message = ""

        for c, char in enumerate(self.decoded_message):
            if char == " ":
                c -= 1
                continue
            self.encode(char, c)
        print(self.encoded_message)

    def encode(self, letter, letterNum):
        if self.decoded_message[self.place_in_message] == letter:
            if self.key + letterNum < 26:
                self.encoded_message += self.alphabet[letterNum + self.key]
                self.place_in_message += 1
                self.key *= 2
            else:
                self.key -= 26
                self.encoded_message += self.alphabet[letterNum + self.key]
                self.place_in_message += 1


MessageEncoder("Hello World", 1)

I'm not sure if I did what you were going for, if I made a mistake please let me know I will make it so that it does whatever.

Sign up to request clarification or add additional context in comments.

3 Comments

There is no reason to make this a class instead of just a function. You should use string.ascii_lowercase instead of writing out the alphabet.
I made it a class to better organize it, also because there was a spam of globals which I just don't like.
And yes, I should have used string to get the alphabet, but he had already typed it out so I just didn't bother.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.