Python: string index out of range

I am piece meal working through the reverse word exercise. I am starting with just breaking down the string and counting the characters in each string and printing each of those characters.

(You will see #greyed out code for the next steps). However, I keep getting one error that I do not know how to correct: IndexError: string index out of range

The ranges are quite important because they determine the placement of the letters in the next step.

The code is here:

thanks,

Eric

you go wrong here:

while count <= len(string)

lets say the string has a length of 5, the highest index is 4 (strings are zero indexed based) so you don’t want the value equal to the length

1 Like

Perfect explanation! Thank you.

I have a similar problem, code is here:

def encrypt(key, msg):
    x = 0
    encrypted = []
    while x != len(msg):
        x += 1
        encrypted.append(chr(ord(msg[x])+key))
    return encrypted

I don’t know how to fix it, it says the error is in

encrypted.append(chr(ord(msg[x])+key))

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.