Loops Over 9000 Exercise

Hello!

I just wanted to check if I understand it correctly why my code was wrong. After looking in other posts I think I get it but…:

This is the exercise.

This is what I had:

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

Am I right in thinking that with the “if sum <= 9000”, I am causing the iteration to stop after the very first iteration because the condition has been met already?

I feel like it’s a ‘stupid’ question. It’s just that until now I did really well, I think, but I am struggling a bit with loops and I wanna make sure I understand correctly.

Many thanks!

It’s never a bad idea to test it if you’re not sure what’s going inside. Have a check of just how the return statement operates too and if actually does what you want.

2 Likes