I'm stuck! (4/15) digit_sum


I'm trying to complete "Practice Makes Perfect", but I'm stuck. I've tried everything already and I still can't pass it. What am I doing wrong?

Here's my code:

def digit_sum(n): #function
x = [] #list
for i in int(n): #looping
if i > 0: a # gr8r than 0
append.i(x) # add every number to the 'x' list
return sum(x) # adding everything in the list



An integer is not iterable. You should be iteraing over a string or a list. Let's look at your code in this light...

def digit_sum(n):
    x = list(n)
    return sum(x)

But if we forego using the sum() method, in favor of our own algo,

def digit_sum(n):
    x = list(n)
    s = 0
    for i in x:
        s += i
    return s