Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List index out of range

Tags:

python

list

I have written a program that makes sure each cookie has more than 5 chips before moving on. However, the line if cookie[i] > 5: seems to be problematic, as it produces a "list index out of range" error. I thought foo = [] created a list with no ending, so I don't get why any number would be out of range in this list. What am I doing wrong?

cookie = []

...

for i in range(0, 11):
    if cookie[i] > 5:
         break
like image 917
awesomeguy Avatar asked Nov 18 '25 14:11

awesomeguy


1 Answers

Try:

len(cookie)

You'll see if you haven't put anything in it that it'll be size 0. With cookie = [] you can continuously add elements to the list but it's initialized empty.

like image 134
silent1mezzo Avatar answered Nov 20 '25 03:11

silent1mezzo