Beginner level - Getting an error "IndexError: list index out of range" in a tutorial program


I am learning a coding language for the first time and that is Python. During my attempt to solve an online coding exercise I got a partially successful output - I got the results correct, but I also got an error message that I want to avoid. Pl. see the attached pic.:

Can you pl. explain why I get this error and how to get rid of it?

Please do not post code in screenshots. Paste the actual code using proper formatting instead.

Your loop condition is while i <= len(List1), meaning the loop will run if i is exactly equal to len(List1). Consider the case were List1 contains only one element, i.e. its length is 1. Will List1[1] work?

1 Like

Wow, lovely. Thanks for a quick and accurate help. I corrected line 4 in the code and it worked:

string = str(input(“Input your string”))
List1 = list(string)
while i<len(List1):

Thanks a ton!

Please wrap code in triple backticks to preserve the formatting:

if True:
    print(''Hello world!')
1 Like