# Why does this give me index out of range?

Can anybody tell me why the following function doesn’t work?
It says “index out of range”

def over_nine_thousand(lst):
sum = 0
index = 0
while(sum <= 9000):
sum += lst[index]
index += 1
return sum

1 Like

This is old, but in case anyone has the same issue think about what happens if `lst = []`?

It’s not, my code looks like this:

``````def over_nine_thousand(lst):
t = 
while sum(t) < 9000:
t.append(lst)
del lst
return sum(t)

``````

And I’m still getting the same error, even though this code prints appropriate answer to console.

What error message are you getting?

What is the objective of the exercise? Are we given a list and asked to determine if its sum, or the sum of part of the list is over 9000?

I’m getting Index out of range.
Objective is to add elements from the list as long till we get 9000. I changed my code a little bit in order for it not to modify the input, so now it looks like this:

``````def over_nine_thousand(lst):
i = 0
t=
while sum(t) < 9000:
t.append(lst[i])
i+=1
return sum(t)
``````

It’s still “Index out of range”

Apart from the logic perhaps having a few wrinkles, the index error would indicate that the list is being reduced to an empty list, or the index is advancing beyond the length of the list and therefore attempting to access `lst[i]` is resulting in the error.

What is the return value supposed to look like? A number? or a boolean?

I’m supposed to sum elements from the input list until they reach 9000. Now I tried even different approach, but still get the same error:

``````def over_nine_thousand(lst):
i = 0
t= 0
while t < 9000:
t+=(lst[i])
i+=1
return t
``````

Honestly, I’m just starting to feel like an idiot Oops, sorry:

I even tried putting in a measure that the code will stop executing when it’s done what it was supposed to do. In another thread ionatan suggested that there is some problem with what I’m indexing so I countermeasured it but that’s still not it:

``````def over_nine_thousand(lst):
i = 0
t= 0
while t < 9000:
t+=(lst[i])
i+=1
if i > len(lst):
break
return  t
``````
1 Like

Okay, I’ve read the instructions and have a idea of what the objective is. Earlier I mentioned the logic having some wrinkles. Is this a good use case for `while`?

Since we are only reading the values in the list and adding them to a counter, it would seem more appropriate to use a `for` loop since we know it will never exceed the length of the list, and thus never raise an index error.

Consider
``````sum = 0          # per hint
for x in lst:    # loop through all the elements
sum += x
if s > 9000: break
return sum
``````

Note that if the list is empty, the loop never runs so the initial value of `sum` is returned.

I believe the reason its giving index out of range is because, even if you don’t see it, the program is checking that you met the requirements of

If the sum of all of the elements is never greater than `9000` , the function should return total sum of all the elements.

You are basically getting an error that does not appear unless testing that particular requirement. A slight modification and the original method in this question will work (checking that the index is less than the length).

``````#Write your function here
def over_nine_thousand(lst):
run_tot = 0
index = 0

while (run_tot <= 9000 and index < len(lst)):
run_tot += lst[index]
index += 1
return run_tot

#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))
``````