python - List index out of range in a random generating program -
i working on program take random words list , combine them 1 sentence keep getting error:
file "test2.py", line 13, in generater print list[z+2] , indexerror: list index out of range
this code:
def generater(): list = ["naji", "gaming", "gameplay", "start", "stuped", "awesome", "fast", "new", "racing", "shoting", "cool", "super"] random import randint x = randint(0,11) print list[x] , y = randint(0,11) if y == x , y != 0 : print list[y-1] , else : print list[y] , z = randint(0,11) if z == x or z == y , z < 8 : print list[z+2] , elif z == x or z == y , z > 9 : print list[z-5] else : print list[z] generater()
your list contains 12 items, valid index range 0 11.
your rand generator returns value between 0 , 10.
10 + 2 more 11 - you're trying access list index off end of list.