 # FAQ: Code Challenge: Loops - Over 9000

11 posts were merged into an existing topic: Can I combine a `while` and a `for` loop?

8 posts were split to a new topic: How do I need to think about solving challenges?

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

``````def over_nine_thousand(lst):
s = 0
for i in lst:
s += i
if s > 9000: break
return s
``````
1 Like

A post was split to a new topic: Why does this give me index out of range?

5 posts were merged into an existing topic: Can I combine a `while` and a `for` loop?

2 posts were split to a new topic: Greater than not equal to 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. 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