Digit_Sum


#1

Error Message:
Oops, try again - Does your digit_sum function take exactly one argument (a positive
integer)? Your code threw a “‘int’ object is not callable” error.

I’ve looked at some of the other forum feeds for this and can’t work out where this is going wrong?! please help! x

def digit_sum(n):
    num_str = str(n)
    for i in range(len(num_str)):
        total = 0
        total += int(num_str[i])
    return total

#2

if i add a function call to your code:

def digit_sum(n):
    num_str = str(n)
    for i in range(len(num_str)):
        total = 0
        total += int(num_str[i])
    return total

print digit_sum(434)

i get 4, so the code is working.

the problem seems to be that total is declared inside the loop, so total will only contain the last digit, given all digits added to it before get overwritten with zero again


4. digit_sum
#4

Thank you, managed to get it working now x


#6