# Digit_sum, how to use floor divider

#1

``````def digit_sum(n):
while len(str(n))>1:
total=0
i=n/10-n//10
total=total+i*10
n=n//10

print digit_sum(123)

it just keep return 0 , which part is incorrect?
thank you so much !``````

#2

Hello @zoezhang1992foxmail,

Your code isn't working because you are resetting the total variable for every loop of the while loop. Try declaring this variable outside of it, that should fix one of the problems.

Also, floor division, also called integer division, returns the integer of the division if the result is a floating point number. For instance...

``````5.0/2.0 = 2.5
5.0//2.0 = 2.0``````

But, in your case, Python2.7 division is automatically floor division because it doesn't return a floating point number unless you specifically instruct it to by using the `float()` function. So, you are basically adding 0 to total repeatedly, and that is why your code isn't working properly.

In order to fix this, I would recommend using a for loop that iterates through the string values of the argument number and adds their `int()` versions into a variable, which is then returned.

I hope this clears out your doubts.

#3

thank you so much
the explanation is very clear.

i have changed my code to
def digit_sum(n):
total=0
while len(str(n))>1:
i=n/10.0-n//10
total=total+i*10
n=n//10
print total
else:
total=total+n