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 = [0]
  while sum(t) < 9000:
   t.append(lst[0])
   del lst[0]  
  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=[0]
  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?

Please post the URL of the exercise in your next reply. Thanks.

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 :frowning:

We still need that link, please.

Oops, sorry:

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/over-ninethousand?action=resume_content_item

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.