4/15 'global name 'digit_sum' is not defined'


#1

I don't at all understand what's wrong with my code, but if someone could explain and help then thank you!
Here's my code:

1def digit_sum(n):
2 lst = []
3 n = str(n)
4 for char in n:
5 lst.append(char)
6
7total = 0
8for i in range(len(lst)):
9 lst[i] = int(lst[i])
10 total += lst[i]
11
12return total

at the bottom it claims 'Did you create a function called digit_sum? Your code threw a "global name 'digit_sum' is not defined" error.', but in the little box it says 'File "python", line 12
SyntaxError: 'return' outside function'.
digit_sum exists, doesn't it?


#2

your code works

check indents

def digit_sum(n):
    lst = []
    n = str(n)
    for char in n:
        lst.append(char)

        total = 0
        for i in range(len(lst)):
            lst[i] = int(lst[i])
            total += lst[i]
    return total`

#3

Thank you! This really did help, I've been stuck on it for half an hour, I didn't think I would get a reply so fast!


#4

you are welcome.
i am struggling with this section, so read Q&A a lot


#5

Check it out this,
def digit_sum(n):
l = []
n = str(n)
for s in n:
l.append(s)
y = 0
for x in l:
z = int(x)
y+= z
return y