digit_sum


#1


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


i have used modulo and division to get the sum but the output gives an error. The error says "Did you create a function called digit_sum? Your code threw a "local variable 'sum' referenced before assignment" error.

Add your code here, between the ```'s!

def digit_sum(n):
    while n > 0:
     sum = 0
     sum = sum + n %10
     n = n / 10
    return sum

#2

digit_sum(0)


#3

You have a variable sum equal to zero all the time. Plus not very good name .Change variable name and take out from while and it's work .

def digit_sum(x):
    result = 0
    while x >0:
        result+=x%10
        x=x/10
    return result