Python IndexError: list index out of range for loop

Question:

So i got this error and i can’t figure it out why.
Can you explain it to me?
enter image description here

enter image description here

Asked By: dominik2256

||

Answers:

you have to use for loop in specific range: make changes your for loop like…

tab = [1,3,4,6,7,6]
def is77():
    for i in range(len(tab)):
        print(tab[i])
is77()
Answered By: Muhammad Shifa

You have to use def like this in python:

tab = [1,3,4,6,7,6]
def is77():
    for i in tab:
        print(i)
is77()
Answered By: Shresth Gour

There are two ways you could fix that.

tab = [1,3,4,6,7,6]
def is77():
    for i in tab:
        print(i)
is77()

or

tab = [1,3,4,6,7,6]
def is77():
    for i in range(len(tab)):
        print(tab[i])
is77()
Answered By: Ishara Dissanayake
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.