Digit_sum need help with it all


#1

Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits.

For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4.

(Assume that the number you are given will always be positive.)

PlLEASE I NEED SOMEONE TO PUT ME THROUGH THIS.


#2

Are you still struggling?


#3

I am still struggling. I decided to start over and start doing them with my own head and only ask for help instead of copying. So I'm stuck at digit_sum.


#4

What do you have so far? Look through the instructions and tell me what you're struggling with?
You know you need to create the función digit_sum so how do you write the function? What parameters do you need?


#5

def digit_sum(n):
for i in n:

That is what I have. How do I return the sum of that numbers digits?


#6

Okay good, before

though, you want to create an empty array called data.
data = []
additionally, you want to create a variable to store a string version of the argument. I used string and just said
string = str(n)
It's called casting, because I turned n into a string. Then you can iterate through string and say for char in string, cast string as a number, int(char), data.append(char), and return (sum(data)).


#7

def digit_sum(n):
    data=[]
    string=[]
    for char in string:
        int(char)
        data.append(char)
        return (sum(data))

This is what I made from what you said. But it's still not working.
I know you must have other things to do and I appreciate you helping me on this.


#8

Hello, are you there?


#9

okay, so we have a function digit_sum which has a parameter n, which will hold a bunch of numbers:

def digit_sum(n):
   print n
print digit_sum(12345)

now the problem is that we can't loop over a integer, look:

def digit_sum(n):
    for i in n:
        print i
print digit_sum(12345)

this will result in an error because integers are not iterable. however, strings are so what we can do is casting n to a string:

def digit_sum(n):
    for i in str(n):
        print i
print digit_sum(12345)

now it works, but doing math with strings sucks, so inside the loop we need to cast the integer back to a integer (you can achieve this with int(), i hope this helps you further

this is a forum, the people helping you can sometimes be occupied with other thigns


#10

def digit_sum(n):
data=[]
string=str(n)
for i in str(n):
print i
print digit_sum(12345)

It's not working.


#11

no, it is not. i was just hoping i could push you in the right direction. you will need to create a variable total, set it to 0, and then add i to total (don't forget the integer casting) and then after the loop, return total

or @kyleaw approach, append the numbers (i) to the list (again, don't forget integer casting) and then after the list is completed, return the sum()


#12

Please I am really trying hard to not go back and copy answers. Please explain it from the beginning where I create the function digit_sum(n)


#13

okay, lets stick to kyleaw explanation, you now have the numbers, you can loop over them:

def digit_sum(n):
    data=[]
    string=str(n)
    for i in string:
        print i
print digit_sum(12345)

now you will need to append the numbers to data after casting them to a integer:

def digit_sum(n):
    data=[]
    string=str(n)
    for i in string:
        i = int(i)
       # append i to data
print digit_sum(12345)

then after the loop finishes, return sum(data), sum is a built in function which will add all the numbers together


#14

def digit_sum(n):
data=[]
string=str(n)
for i in string:
i = int(i)
data.append(i)
print digit_sum(12345)

This is what I made up


#15

you still missed the last step:


#16

def digit_sum(n):
data=[]
string=str(n)
for i in string:
i = int(i)
data.append(i)
return sum(data)
print digit_sum(12345)

This worked, thank you very much. I appreciate it a lot.


#17

I have one question, why did i have to add data=[], also why did we use i=int(i) when we had string=str(n), I am referring to the i and n.


#18

well, you can't append items to a non existing list.


#19

If we're going to use the built in (which defeats the practice, btw) then we may as well go all out:

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

It's what the step-by-step approach would eventually refactor to. For this exercise, I'm leaning toward the manual accumulation of total.

From the above we see that we don't need to cache a string, just declare it in the statement: We'll be iterating the transient string value.

total = 0
for x in str(n):
    total += int(x)
return total

I'm not really fond of the term data to describe a single number. It is a plural term That is why I used n.


#20

i know, the problem this creates however is that 2 people suggest different approaches, since kyleaw was first, i decided to go with her approach, i send her a message about the approach you suggest:

total = 0
for x in str(n):
    total += int(x)
return total