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

#1

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:
tot=0
for i in str(n):
tot+=int(i)
print digit_sum(1234)``````

#2

Answered in post three by @stetim94

#3

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

#4

Yup, saw it after you posted.

#5

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

#6

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

#7

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