IndexError: list index out of range when looping through a list in Python
I am learning Python and trying to print every item in a list using a for loop with range(). However, my program stops with an IndexError. Here is my code: numbers = [10, 20, 30, 40] for i in range(len(numbers) + 1): print(numbers[i]) When I run it, the first four values are printed, then I get this error: Traceback (most recent call last): File "main.py", line 4, in <module> print(numbers[i]) IndexError: list index out of range What I expected: the program prints all four numbers (10, 20, 30, 40) and then finishes without errors. What I tried: I changed the list values and restarted the progr
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.