# Looking for logical explanation

#1

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

cos this is what i wrote initially

``````  def digit_sum(n):
return digit_sum(n)

``````

#2

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?

#3

Do you have your own code, as well?

#4

yes, this is his code:

``````  def digit_sum(n):
return digit_sum(n)
``````

#5

Ooh, that’s not a good thing.

#6

#7

#8

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

#9

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

#10

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.

#11

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

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

#12

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:
digit_sum(value)
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…

#13

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?

#14

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

#15

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

#17

guess this is where am missing out. Thank you

#18

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

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

#19

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