0

I am making an encrypter in Python and it keeps sending me a "string index out of range" message.

input = input("").lower()
letters =["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"]
message = []
encryption = []
letter = 0
code = 1
wordletter = 0
word = "baa"

for i in range(len(input)):

    code = 1

    wordletter += 1

    if wordletter > len(word):
        wordletter = 0

    code = letters.index(word[wordletter])

    if input[i] in letters:
        letter = letters.index(input[i])
        letter += code
        message.append(letter)
    else:
        message.append(input[i])

for i in range(len(message)):
    encryption.append(letters[message[i]])

It says the error is on line 19 - like this.

Traceback (most recent call last):
  File "//x17/StudentHome/2023/a.nachmany16/Encrypter.py", line 19, in <module>
    code = letters.index(word[wordletter])
IndexError: string index out of range
>>> 

Can someone help me?

0

1 Answer 1

1
if wordletter > len(word):
    wordletter = 0

Since Python uses zero-based indexing, the condition should be changed to >=.

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

Comments

Your Answer

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