digit_sum returns 10, but no pass


def digit_sum(n):
n_sum = 0

while n > 10:
    n_sum = n_sum + (n % 10)
    n = (n // 10)  
    n_sum = n_sum + n
return n_sum

print digit_sum(1234)

digit_returns 10, as shown in error message, but no pass.
Oops, try again. Your function fails on digit_sum(1000). It returns 10 when it should return 1.


1 + 0 + 0 + 0 = 1


Thanks Jonatan.

I got it, now. I thought it was something I did; I did not realize it was Codecademy, trying to put me back on track.

Spunkmoxie, T.S.


Will you please tell me what you understood....i am getting same problem but i din understood....



When Jonatan responded "1 + 0 + 0 + 0 = 1" to my question, I realized Codecademy was telling me my solution would not work on the number 1000, even tough it would work on 1234. I knew I would have to modify my code to handle those zeros. A minute later I had the answer, and I am certain you will, as well -- you could not have come this far, if that answer is not in you.

Good Luck,


 def digit_sum(n):
        sum_n = 0
        while n>10:
        return sum_n

But i have tried your code and still it is giving me error message on digit_sum(1234)
Listen, if you got the answer then tell me...i am trying to solve this problem and i had so many attempts...



If you are using, my code you will get an error. It is the code I posted, when I was having the same problem you are having, now. It worked for 1234, but not for 1000. Don't test on 1234 anymore, test on 1000. Put in some print statements, so you can see what is happening. Understand what % and // are really doing. Then, put in an if statement to handle the zeros; you don't need them.

Spunkmoxie D.H.