Digit_sum


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


It is giving me the error message of Oops, try again. Did you create a function called digit_sum? Your code threw a "local variable 'a' referenced before assignment" error.


result = 0
ans = []
a = 0
def digit_sum(n):
    while n != 0:
        result = n % 10
        ans.append(result)
        n = n // 10
    for x in ans:
        a += x
    return a


#2

when the exercise validates your code, only the function is called, not the whole script

So place the variable inside your function


#4

Thank you, I put all the variables inside and now it works.


#5