Assignment: Make a function that adds numbers in a list until the sum is greater than 9,000. Once the sum is greater than 9,000 stop adding and print the sum.
Here’s my code:
def over_nine_thousand(lst): nums_added = 0 if len(lst) == 0: return nums_added else: while nums_added <= 9000: for n in lst: nums_added += n return nums_added print(over_nine_thousand([8000, 900, 120, 5000]))
The while loop under the else statement is the part causing me problems. It looks to me like it should terminate as soon as nums_added is greater than 9,000. But every time I run the code I get 14,020 (the sum of all numbers in the list.
How do I get the program to exit the while loop? I think I could test with a bool but that seems less elegant than how I set it up. That said, how I set it up isn’t working, so yea.