[solved] digit sum


#1

a = []

def digit_sum(n):
    if n>0:
        x = str(n)
        y = int(x)
        a.append(y)
        z = sum(a)
        return z
       
digit_sum(1234)

Hello.
So i created this, it works fine till the a.append part: i get a list with [1234]
But when i sum it i get this error:
Your function fails on digit_sum(434). It returns 1668 when it should return 11.

Sum is new to me so maybe i did not use it correctly.


Digit_sum
#2

using a built in function like sum() does take out a bit of the fun. Try without sum, simply declare a variable total before the loop, in the loop, add the number to total

The exercise will validate your code works correctly, this is achieved by calling your function. This means, only your function gets executed, not the whole script. So if multiply function calls happen, the list doesn't become empty, place the list declaration inside the function


#3

total = 0
def digit_sum(n):
    if n>0:
        a = []
        x = str(n)
        y = int(x)
        a.append(y)
        for i in range(0, len(n)):
            total = sum(n)
            return total
        
       
digit_sum(1234)

So, i followed your advice, but i get an error:
Traceback (most recent call last):
File "python", line 13, in
File "python", line 8, in digit_sum
TypeError: object of type 'int' has no len()

1.I think i did not covert the number into list correctly.
2.Also i don't know how to sum the list without using sum.


#4

i would place this:

total = 0

inside the function, we talked about this.

this lines:

        y = int(x)
        a.append(y)

are redundant. you still have n which contains the integer, and we loop over x (string of the number) anyway, so we don't need a list. So i suggest to remove this two lines.

a.append(y) would simply result in: [1234], not very useful, we still have integers (which are not iterable). So i suggest not to use the list at all

okay, then here:

for i in range(0, len(n)):

n is a integer. len() doesn't work for an integers. we convert our integer to a string, we can use the string to loop over our number. Personally i would just get rid of range() as well, but that it your choice.

okay, now that we loop over our string (did you figure this out?) we can start adding total, we can simply use + for this, basic math, probably taught somewhere in the first track.

here is an example:

total = 0
total = total + 5
print total # will print 5

that should help you.

Also, a f unction ends the moment a return keyword is reached, this currently happens in the first iteration of your loop, so the loop simply breaks at the end of the first iteration.

Place the return outside the loop so the whole loop can run


#5

def digit_sum(n):
    if n>0:
        total = 0
        x = str(n)
        total = total + int(x[0])+int(x[1])+int(x[2])+int(x[3])
    return total        

       
digit_sum(1234)

Sorry, i still can't figure it out :[


#6

why did you remove your for loop? you can simply use this:

for i in x:
    # add i to total, don't forget integer cast

to loop over all values in x (which is a string of your integers)


#7

Thank you for your kind assistance and help.
Brg


#8