I passed the [digit sum] but no print out


#1

n=345
def digit_sum(n):
    result = 0
    for i in str(n):
        result += int(i)
    return result
    print result

i got passed the quiz(digit sum)
but when I input n=345 for the purpose of test
my function has no print out.

Is there any error at my code ?


#2

This line is still in the scope of the function block (and after return so unreachable). Remove indentation.

Now we see that result is undefined. The variable is not returned, only the value. Try to print the return value...

print (digit_sum(n))

Recognize that n = 345 is not the same n that is defined in the local scope of the function. It is only copied to that variable. Their names are the same, but that is all. What happens to n inside the function does not reflect on the n in global scope.


#3

Really thanks for your help.
Have a good day...


#4

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