Python IndexError: list index out of range for loop

Viewed 30
3 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()

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()

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()
Related