# Digit_Sum

#1

Hello,

The correct code to get the digit sum is below

``````def digit_sum(x):
total = 0
while x > 0:
total += x % 10
x = x // 10
print x
return total
``````

However, if I modify the code to be

``````def digit_sum(x):
total = 0
while x > 0:
x = x // 10
total += x % 10
print x
return total
``````

It is wrong. Why is that?

Lets assume x = 25
then x = x//10 = 2 (first digit)
then total += x%10 = 5 (second digit)

How does the sequence of the code work?

Please help

#2

It should work for all numbers, not just 25 (and working for 25 doesnâ€™t mean it works for all numbers)
And besides, you donâ€™t keep the 2, so it wonâ€™t work for 25 either

You should write the code in a way that it is convincing that itâ€™ll behave correctly for all numbers

Most likely this means doing one digit per iteration in your loop, rather than doing both 2 and 5 (in the case of 25) during the first iteration

I suggest executing your code manually and observe what you end up doing.
I also suggest observing how you would do the task manually, and then model your code after that so that the code does the same thing that youâ€™d do.

#3

Hello thereâ€¦ I broke my head on how to do this one as wellâ€¦ but your code seems so confusing I barely understand what does it meanâ€¦
what I did to make sure itâ€™ll work with any positive numberâ€¦ is to simply change the number into a stringâ€¦ (because the FOR function reads every letter if the value is a stringâ€¦ )
then I changed it back to integer when I summed it up with my â€śtotalâ€ť

#4

The string conversion from int necessarily has to do the same thing, so it is arguably more complicated (does the same steps and more) - but matches how a human would do it, we read one digit at a time, not the whole number and do division

Itâ€™s very simple though, just consider what happens when you divide by 10 without keeping decimals, and what happens when you multiply it by 10 again and subtract from the original (same as computing remainder)

#5

ahhâ€¦ Iâ€™m confusedâ€¦ tried to read his code for the third timeâ€¦ clueless stillâ€¦

``````def digit_sum(n):
result = 0
for number in str(n):
result += int(number)
return result

``````

wellâ€¦ this is my codeâ€¦

#6

Try executing it manually and observe what you end up doing.
You should know what each operation does there anyway

#7

I got it nowâ€¦
in my opinion it is very different from my approachâ€¦ and seems like a more complicated method to think aboutâ€¦

now you said that my method is more complicated because it takes more stepsâ€¦ turning it into a stringâ€¦ and than back into integerâ€¦
does it mean itâ€™ll take more computational power?
may be a stupid questionâ€¦ but iâ€™m genuinely interested

#8

When you do str(number), the string conversion has to divide by 10 to pop off the digits and produce the string

So it does the same, plus some more

So yes, more computing, but itâ€™s insignificant (more but not in a way that matters)

Personally Iâ€™d do it like you:

``````sum(map(int, str(1234)))  # 10
``````

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.