Looking for logical explanation


can someone please explain the logic behind this code …

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

cos this is what i wrote initially

  def digit_sum(n):
    return digit_sum(n)


I am not a huge fan of this suggested solution, it can be rather overwhelming for new learners. I would have preferred if the solution used a string to loop over the number and add each digit to total

lets say we want to get the digit sum of 434 (which is 11, like so: 4 + 3 + 4).

then x % 10 gets the last/right most digit (4) and add it total. Then dividing by 10 will remove the right most digit. this will go on until x becomes zero

how is your solution going to get the sum of the digits?


Do you have your own code, as well?


yes, this is his code:

  def digit_sum(n):
    return digit_sum(n)


Ooh, that’s not a good thing.


which is why i asked:


@iamstyles, how is your math?


but i believe its the logic am trying to understand since i was thinking the digit_sum() functioned same as floor() and ceil() functions


so if the digit_sum() is used in the integer, is the while loop going to loop for 434 times or what.


That would set off some confusion, for sure. The mathematical approach you posted is not the naive solution most beginners would reach for. In fact if it wasn’t found here, they would likely never come up with it on their own.

Intuitively one would think digits, and since a number is not iterable, meaning we cannot take out one digit at a time, we need to cast it to an iterable, either a string or a list, so that the individual digits can be accessed.

for i in str(n):

Now i is one digit each pass.


used in the integer, what does that even mean?

lets look at it:

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

print digit_sum(434)

see how the function gives the sum of the digits?

thus the while loop will loop over each digit of the integer


i tried this as well but had a little confusion along the line

def digit_sum(n):
  convert_to_string = str(n)
  for value in convert_to_string:
  return digit_sum

trying to convert the integer to string and iterate over it one after the other.

what is missing in the logic as well…


the digit_sum function has as purpose to get sum of an integer, you can’t just call the function inside itself hoping that some magic happens, you have to write this logic

why do you think the suggested solution uses total and is increasing total?


That is a recursion. You need to initialize a total to 0, then add to it successively each iteration.


that’s what am trying to understand, because after increasing total it also finds the absolute value of x again before the next iteration


guess this is where am missing out. Thank you


// is floor division. There is no absolute value in this or any solution to the problem.

>>> from math import floor
>>> print (floor(1234 / 10))
>>> print (1234 // 10)


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