# Exercise tells me I am wrong when I get the answer right?

Hello everyone,
I am doing some coding exercises and I got a challenge to build a function that basically counts all the way to 9000. If 9000 is exceeded than it will return the current sum in the function.

I built my code like this:

``````#Write your function here

def over_nine_thousand(lst):

sum = 0

for i in lst:

if sum <= 9000:

sum += i

else:

return sum

#Uncomment the line below when your function is done

print(over_nine_thousand([8000, 900, 120, 5000]))
``````

when the function was called I always got the answer that was expected (according to their instructions. 9020). However, when clicking â€śCheck Answerâ€ť I got the following message: `over_nine_thousand([8000, 900])` should have returned `8900` , and it returned None

And when copying/pasting their way of doing the exercise which is:

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

it said the answer is correct. Am I doing anything wrong?

Can you provide a link to the exercise? There might be something in the wording. Particularly, whatâ€™s supposed to happen if the sum doesnâ€™t exceed 9000.

The CC error message suggests that something should be returned.

Of course.

Exercise 2

Yea so the key is in the algorithm thatâ€™s outlined.

• Still within the loop, check if the sum is greater than 9000. If it is, end the loop
• Return the value of the sum when we ended our loop

This means, your function should return a value no matter what. It has an extra condition that if it exceeds 9000 it must return it right there and then. BUT, if it doesnâ€™t, it should still return some sum.

So something like this?

``````def over_nine_thousand(lst):

sum = 0

for i in lst:
if sum <= 9000:
sum += i
else:
break

return sum
``````

Now instead of returning something within the loop itâ€™ll return something when the loop has ended thus always returning something

Yea, that should work.

The thing with python is itâ€™s often easy to write weird functions. I usually try to be extra careful to make sure something is returned even through the conditional blocks. (Because ideally either functions return something even if itâ€™s an error term, or nothing at all).

1 Like