-1
notas = [10,5,6,7,8]
for i in range(0,len(notas)+1):
print(notas[i])
Made the mistake:
line 4, in <module>
print(notas[i])
IndexError: list index out of range
-1
notas = [10,5,6,7,8]
for i in range(0,len(notas)+1):
print(notas[i])
Made the mistake:
line 4, in <module>
print(notas[i])
IndexError: list index out of range
0
Your list has 5 elements, so len(notas)
will return integer 5, being first position 0 and last 4.
You defined a loop of repetition so that:
for i in range(0,len(notas)+1):
...
That is, your range
will go through the value set [0, len(notas) + 1[
(set opened on the right by language definition). Like len
is 5, will stay [0, 6[
, that dealing with integers will be the set (0, 1, 2, 3, 4, 5)
.
That is, in the last iteration, the i
will be worth 5 and will try to access position 5 of your list, which does not exist.
The right thing would be without the + 1
:
for i in range(0,len(notas)):
...
But better than that, you can scroll through the list directly without using the control variable i
and without depending on the list size:
for nota in notas:
print(nota)
To facilitate the identification of these types of problems I recommend reading:
Browser other questions tagged python for
You are not signed in. Login or sign up in order to post.