Python loops

This is the task I am solving:
Create a function named over_nine_thousand() that takes a list of numbers named lst as a parameter.

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 .

For example, if lst was [8000, 900, 120, 5000] , then the function should return 9020 .

My question is why this works:

def over_nine_thousand(lst):
  sum1 = 0
  for num in lst:
    if sum1 <= 9000:
      sum1 += num
  return sum1

but this doesn’t:

  sum1 = 0
  for num in lst:
    while sum1 <= 9000:
      sum1 += num
  return sum1 ```

I run the function on this list : print(over_nine_thousand([8000, 900, 120, 5000]))
and the first one returns 9200 and the second one 16000.

the code that doesn’t work simple add the first number of the list to sum1 until it reaches 9000 with courtesy of the while loop. The while loop will prevent the for loop from ever reaching the second number in the list.

you can see this when studying the output of the second, 16000 is simply 8000 + 8000.

You ask why, but would you argue that it should?
Would you argue that they should behave the same, would you argue that it’s wrong to use if and that it should instead be while?
Have you understood the instructions, what does the process look like when you carry it out manually?

When you say why it should be because you see it some other way shouldn’t it?

Do you know how each thing in your code behaves? Does that mean you could execute it in your head? If you use prints to write out what is being done, does it execute the same way as you thought it would, or have you perhaps misunderstood what something is supposed to do?

So there’s more that you can do to help others understand your question, and to isolate the question to just a single operation, and quite likely figure it out yourself by making your question more well defined.