digit_sum returns 10, but no pass


#1

def digit_sum(n):
n_sum = 0

while n > 10:
    n_sum = n_sum + (n % 10)
    n = (n // 10)  
else:
    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.


#2

1 + 0 + 0 + 0 = 1


#3

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.


#4

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


#5

Megablaster02630:

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,

SMTS


#6
 def digit_sum(n):
        sum_n = 0
        while n>10:
            sum_n=sum_n+(n%10)
            n=n//10
        else:
            sum_n=sum_n+n
        return sum_n
    digit_sum(1234)

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


#7

megablaster02630,

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.