I am working on this exercise and I have something I don’t quite understand.

Here is the code:

#Write your function here
def over_nine_thousand(lst):
total = 0
for n in lst:
total += n
if total > 9000:
return total
break
return total
#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))

This is a correct answer, but I was wondering if I could do it with a while loop. Something like this:

#Write your function here
def over_nine_thousand(lst):
total = 0
while total <= 9000:
for n in lst:
total += n
return total
#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))

I get a result of 14020. Here are the specific instructions:

"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 ."

The problem is that it did not return 9020. Something did not work with the while loop. Can someone explain???

You can always throw in some print statements or similar to start debugging. Consider how your for loop typically operates and what the original answer does to deal with the for loop.

Note that the while loop condition is only checked at the start of each iteration.

Oh ok I understand, the for is inside the while. This causes the for to run through and add all the numbers and add them all before the while goes again. So in this case, the if loop would be best. Thanks!!!

Hi! I have the same problem with this challenge. This is my code:

#Write your function here
def over_nine_thousand(lst):
sum = 0
for num in lst:
while sum <= 9000:
sum += num
return sum
#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))

I used the while loop inside the for, and I’m getting 16000 as answer, I would like some help to understand why it is wrong, and how I get 16000, please. I appreciate it!

First iteration of your for loop, num will be 8000. sum is 0, so the condition is true and you enter the while loop and add 8000 to sum. Since you chose a while loop, so the condition is again checked. sum is 8000, so the condition is true and you do another iteration of the while loop. num is still 8000, so you add it to sum. sum is now 16000. The condition of the while loop is checked again. sum is 16000, so condition is false and you exit the while loop.

Second iteration of for loop, num will be 900. sum is 16000, so the condition is false and you skip the while loop.

Third iteration of for loop, num will be 120. sum is 16000, so the condition is false and you skip the while loop.

Fourth iteration of for loop, num will be 5000. sum is 16000, so the condition is false and you skip the while loop.

Finally, you return sum which is 16000.

You should think about whether you actually need two loops. If you don’t need two loops, then what do you need to change? If sum exceeds 9000, then you should return sum immediately without iterating over other numbers of lst.

Test your code for three situations and see if you get desired output.
Situation 1: lst is empty
Situation 2: Numbers in lst add up to more than 9000
Situation 3: Numbers in lst add up to less than 9000