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


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)
7total = 0
8for i in range(len(lst)):
9 lst[i] = int(lst[i])
10 total += lst[i]
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?


your code works

check indents

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

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


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!


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


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