Codecademy Forums

How can we exit a loop?

This series of code worked after I took the elif statements out of the for loop and changed them to if statements.

Thank you for pointing me in the right directions

def over_nine_thousand(lst):
  total =[]
  for i in lst:
    if sum(total) >= 9000:         
      return sum(total)    
  if len(lst) == 0:
      return 0
  if sum(lst) < 9000:
      return sum(lst)
1 Like

…What’s the sum of an empty list?
And, if you’ve already determined that the total sum is at least 9000, do you need to test again whether it’s less than 9000?

Doesn’t it say OVER 9000? You seem to accept equal as well

I’m pointing out a whole lot of really obvious stuff that you’re very capable of reasoning about - most of the mistakes here come from doing reasoning that you’re already great at, but you’re doing it after you’ve already written it. If you reason out what should happen before writing it then those ideas are still in a form where you know exactly how to deal with them

You might for example start by writing out what should happen in plain english. yes it’s a bit silly but also forces you to go through it and mainly I only say write because that’s the easiest way to explain it, the important thing is thinking it through first

turns out we humans are pretty good at doing these manually, so that’s something you can use to process the problem before you move on to unfamiliar territory (code)


The directions on the exercise:
The function should sum the elements of the list until the sum is greater than 9000 . When this happens, the function should return the sum. If the sum of all of the elements is never greater than 9000 , the function should return total sum of all the elements. If the list is empty, the function should return 0 .

I wrote if statements that coincided with what the directions were asking. My experience is only a few months now with coding so I am quite literal when following directions.

I removed the equal sign on my condition.

1 Like

You may also find that if you do this manually, you won’t be trying to remember the individual numbers but instead only the current sum.

You would catch yourself doing this inefficient thing if you did it manually, yes?

Programming should really stay in that part of your reasoning. Same old every-day logic applies.

1 Like

I do write some of my code manually because it helps me think(especially my mistakes, when I am close to desired output).

If I don’t recognize something it is probably because I have not conditioned my brain through experience to do so. as I am still learning (albeit, by myself), getting this feedback is important.

1 Like

Not sure why the hint asks us to use break, when the following seems to work:

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

If I give that something similar to a list that is infinite, then it won’t terminate since it doesn’t exit early

1 Like

Isn’t that what the line

if (lst_sum < 9000):

checks for?

1 Like

The loop still continues.

Here’s such a value that you can try:

from itertools import repeat


oops, should be repeat(1), not repeat([1]), apparently. That’s what I get for posting things without testing them.

1 Like

Oh and you have a bug. Probably, anyway, didn’t actually try. This should result in 9001:

print(over_nine_thousand([1] * 10000))
1 Like

Hello! First of all, the return is wrongly intended. It should be exactly below the if statement. But even if you do this , the code will display [8000, 900, 120] as a solution. This is because, you chose to return a list, instead of a variable.

total =

Considering what append.() does, can you now unterstand the error?

I please you to correct me if I’m wrong, i consider myself as an absolute beginner and it’s my first time advising someone else.

Hope i helped you. Happy coding!