FAQ: Code Challenge: Loops - Over 9000

This one is tricky, I’ve got the correct answer by using 1 conditional and relative return statement inside the loop and 1 with another return statement outside. Both if statements are set to the sum being one higher and one lower than 9000. So no need for break statement in this case.

I got to this solution at it was accepted:

def over_nine_thousand(lst):
sum = 0
while sum < 9000:
for number in lst:
sum = sum + number
if sum > 9000:
break
return sum

1 Like

Here’s my solution

def over_nine_thousand(lst):
  vegeta = 0
  
  for num in lst:
    vegeta = vegeta + num
    if vegeta > 9000:
      break
    print(vegeta)
  
  return vegeta

Hey @bitplayer30925! Please don’t post the correct answer to the exercise! If you do do it, at least blur it because people are supposed to find the answer to the exercise themselves.
Thanks!

1 Like

Thanks for the call out, you’ve got it. :+1:

3 posts were split to a new topic: Can I combine a while and a for loop?

3 posts were split to a new topic: Bug in Python Loop Challenge (reported)

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

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.