FAQ: Code Challenge: Loops - Over 9000

Sticking with the use of loops this time instead of shortcutting:

def over_nine_thousand(lst):
  total = 0
  for i in lst:
    if total <= 9000:
      total += i
  return total

There’s probably a shorter way to accomplish this, though.

7 posts were split to a new topic: What are the differences between if and while?

6 posts were merged into an existing topic: How can we exit a loop?

A post was split to a new topic: Is my code valid?

2 posts were split to a new topic: Can you help me understand why this doesn’t work?

5 posts were merged into an existing topic: How can we exit a loop?

SOLVED : Is anyone else getting “Failed to test your code” error? I’m literally unable to finish this exercise because whatever code I type I’m getting this error. I was already frustrated with Codecademy, now I’m furious.

Ok, I had to restart my windows and delete my browser cache, it did the trick.

But now I’m having another problem, I solved the exercise, I have 9020 printed in my console but I’m getting following error:

list index out of range

This is my code:

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

Rather than appending numbers to a list and summing the numbers, add the number to another number

You don’t need to modify the input list, you only need to read from it

You should usually not make structural changes at the front of a list, a list supports insertion and removal at the end, doing things at the front is equivalent to copying the whole thing. If you for some reason do need to make changes at the front, then reverse the list first so that you’re working from the side that the list supports, or use a different data structure that supports what you’re doing. Though, like mentioned, you only need to read from the list.

I still got the same error even when I only read lst:

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

Well if you get index out of range, then what index did you use, should you have used that, what is the maximum index for the list that you have?

There’s nothing in your code that considers the size of the list

and,

total = 0

total += 3
total += 4
total += 7
...

Your list t serves no purpose. Use int.

What do you mean it serves no purpose? As long as the sum of elements on list t is less than 9000 code takes element from lst at index [i] and adds it to the list t and adds one to my counter i. And I get the appropriate result - 9020.

What do you mean by considering list size? When the sum of elements on list t is bigger than 9000 the code inside a loop stops being executed.

I’m sorry if my questions seem stupid but I’m really a beginner.

You do not need a list, you need a sum. A sum is one number. You are storing your sum in a list broken up into pieces, but if you look at how you are accessing it, then it’s always treated as an integer. You add to it, you look at the total. It’s one number.

If you’re going to use index 3234 then you better make sure its size is at least 3235

You are using indices, but not checking the size, so you have not checked whether the indices you are using are valid. So when you are looking at specific locations, do you assume that it has some specific size? Maybe you assume it’s infinite? Maybe you assume it has enough numbers to get you past 9000. But what says that’s the case?

You have an error message about using an invalid index. So you would consider what index you used and compare that to what you meant should have happened. Maybe you shouldn’t have used that location, maybe you should have checked something first, maybe the list was supposed to be different, and so on, compare what happened to what you meant.

By saying “you are using the indices” you mean’t that I’m using a list to create final sum? I’m sorry but I’m getting lost in lingo.
I changed my code to avoid using list, now I’musing integer that is getting modifyed with every iteration of the loop but I still get the “Index out of range” error :frowning:

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

You’re only indexing one thing, so no.

well yeah but why would it have been fixed? You’ve still not looked at what happened. You need to look at which operation it was and which values were involved and think about things like what happened before and what should have been instead.
You do not get an error and say “oh, that’s too bad”, you have to investigate, find out what happened.

A car mechanic opens up the hood of the car to look into the engine to figure out what’s going on it it.

Your program is something that you fully control, so when something goes wrong in it, this is within your domain, this is within your reach and everything here is visible to you.
Pointing your eyes in its direction isn’t enough to tell you things, but if you decide that you want some information, then that is something that you can make your program emit, you need to make measurements.

It doesn’t say at which operation it happened, error message is not in the console (where it would tell me which line is causing error) but in a little window that pops up and I can close it. In the console I’m having 9020 which is the supposed answer. So unlike car mechanic I have no way to tell at which point my engine is failing, all I know is that the car wont start.

How can I make measurment in a situation like this?

Then test your function yourself. Ignore what the car owner is telling you and drive the car around to observe for yourself.
Pick some input values that it should handle. You may want to read the instructions again to remind yourself of what possible inputs there can be. Try to pick extreme inputs, like very big or very small, and anything else that is polarizing with regard to decisions that your program makes, so for example different things happen around 9000

If your program has information that its user should see, how do you show it to the user?
A measurement is a transfer of data, so how do you move data from your program to you?

But also, there’s only one place where you can produce this error, because there’s only one place where you do that thing. And like I’ve been saying, there’s no checking whether indices that you are using are reasonable to use for the list that you’ve got. Your function contains no attempt to find out whether it has reached the end of the list.

I don’t think it was covered in this course so far, the only option I know to show something in a console is a print method.

I tried tweaking the input, i puit very small numbers, I put very large numbers and my function still does what it was supposed to do, adds number until they reach 9000. The only problem is that poppoing up window with “index out of range”.

Maybe it will help if you tell me what is it that is out of range? Is it the number I get as a sum and it’s out of range because it’s bigger than 9000? That’s my only idea about something in my code that could be out of range.

So if you wanted to know what size the list is and what index you used, you wouldn’t be able to print it?

What input did you use? What’s the smallest possible input that can be given to your function?

There’s only one place in your code where you’re doing indexing, it’s no use that I tell you, you can read your code. You need to know what operations you are carrying out (otherwise who’s writing the code) so where are you using an index?

Anyway I’m hungry so I’ll leave you with this:

The smallest valid input here is an empty list.
What will your function end up doing for that?

And also, keep in mind that this has pretty much nothing to do with getting this specific code to work, this is about general debugging and gaining control over what you’re writing, the problem is only a vehicle for that, that’s what exercises are. It’s time well spent.

Question: Can someone please explain why the function below returns three different results when the “return” line is indented at different levels?

Thanks in advance.

#Output is 9020
def over_nine_thousand(lst):
  sum = 0
  for number in lst:
    sum += number
    if (sum > 9000):
      break
  return sum
print(over_nine_thousand([8000, 900, 120, 5000]))
#Output is 8000
def over_nine_thousand(lst):
  sum = 0
  for number in lst:
    sum += number
    if (sum > 9000):
      break
    return sum
print(over_nine_thousand([8000, 900, 120, 5000]))
#Output is None
def over_nine_thousand(lst):
  sum = 0
  for number in lst:
    sum += number
    if (sum > 9000):
      break
      return sum
print(over_nine_thousand([8000, 900, 120, 5000]))
1 Like