# 4. digit_sum - How to do the challenge?

#1

Okay so I figured out how to get the answer they are looking for

``````def digit_sum(n):
x = str(n)
n = 0
for y in x:
n += int(y)
return n``````

However, I looked into the hint box and there is a challenge where it asks you to use modulo and floor division

If you're looking for a challenge, try this: to get the rightmost digit of a number, you can modulo (%) the number by 10. To remove the rightmost digit you can floor divide (//) the number by 10. (Don't worry if you're not familiar with floor divisionâ€”you can look up the documentation here. Remember, this is a challenge!)
Try working this into a pattern to isolate all of the digits and add them to a total.

I am wondering how this would be done? My thoughts is that you would probably want to use a for/if-break loop. to take the numbers seperately, add them into a variable and break once your it hits a 0. But I have no idea how I would get it to loop once as

``x % 10``

then the next time it loops do

``(x // 10) % 10``

and then continue to add ") // 10)" until it breaks by receiving '0'.
I'm not sure if I have completely over thought this or if I am totally on the wrong track. Help would be appreciated more than a straight answer, but if you can explain the answer that would be fine aswell.

#2

Well, we don't know how many digits are in the given number so it might be just easier to use the `while` loop.

And you want to iterate as long as `x` is different than `0`.

And in every iteration you just need to add to the `sum` the last digit (using `x % 10`) and cut it off (using `x // 10`).

#3

Wow!!! Thank-you. That helped out so much!
The way I worked it out with your advice was I figured out that all I wanted it to do was

``````n = 1234
x = n % 10
y = n // 10
z = y % 10
a = y // 10
b = a % 10
c = a // 10
d = x + z + b + c``````

Basically wanting it to save the n % 10 each time, and then continue doing the loop on whatever the answer for n // 10 was. So I wrote this

``````def digit_sum(n):
p = 0
while n != 0:
x = n % 10
n = n // 10
p += x
return p``````

Does that seem simple enough? Is there another way around this problem? the while loop was definitely more simple than my for/if-break idea. So is that the same way you would solve the problem?

#4

You're very welcome

Very good code! I have only a one, minor comment - object `x` is not really necessary. You can just do:

``````def digit_sum(n):
p = 0
while n != 0:
p += n % 10
n = n // 10
return p``````

So is that the same way you would solve the problem?

This is a perfectly fine solution, really. I would probably use a list comprehension and the `sum` function:

``````def digit_sum(n):
return sum([int(x) for x in str(n)])``````

This code might seem complicated, but it's really not, you will learn about list comprehension in the section advanced topics in python.

#5

Oh that makes so much sense, I always seem to add too many things. I'll finish a lesson and then come on to the forums to see people finishing it and they always seem to shrink my code in some way!

I think I partially understand what it is doing, I am excited again to move on and eventually get the the advanced topics!

Thank-you again.

#6

good code, and why we need [ , it`s working like it too:

``return sum(int(x) for x in str(n))``

#7

@jefferson95 You're very welcome, again Good luck with next exercises!

@chipninja36564 Thank you

There is a small difference between my solution and code posted by you. I have used a list comprehension and your code uses a generator expression.

The first one will create a new list in the memory. The second one creates a generator that yields the next elements of the list, but the execution of the generator requires a separate execution frame.

When we are concerned about the memory usage (very long list) we should use the generator expression. If this is not the case -> list comprehension is usually faster.

#8

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