The code is working but the error is shown regardless :/


Oops, try again. Did you create a function called digit_sum? Your code threw a "local variable 'tot' referenced before assignment" error.

no error , should arise.

def digit_sum(n):
    if n>0:
        for i in str(n):
    return tot
print digit_sum(1234)


Answered in post three by @stetim94


the exercise is going to validate your function with different values, if the value is not greater then zero, tot doesn't get defined, so you can't return it (since it then not defined)

define tot inside the function, but before the if condition, so it always gets defined, even for numbers smaller then zero

@825orion, if you define tot before the function, to won't get reset to zero when the exercise calls the function multiply times


Yup, saw it after you posted.


thanks! it's done . Can you tell me how to know such intricate parts in code writing , any worthy read ?


programming a lot helps, programming is acquired by experience and a lot of practice.

Being around on the forum a lot to help other students gave me good insight on how exercise validation is done, that also helped a lot in this case


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.