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
For example, if
[8000, 900, 120, 5000] , then the function should return
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.