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
        n = n // 10
    for x in ans:
        a += x
    return a


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

So place the variable inside your function


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