Digit_sum


#1

I have been at this for 30mins now and can't figure out the 'syntax error'. Please help.

n = input("?")
def digit_sum(n):
    for digit in str(n):
        total = 0
        return total += int(digit)
    print total 

File "python", line 5
    return total += int(digit)
              ^
SyntaxError: invalid syntax

#2

You are attempting to return a statement. The only way we can do that is if the statement is wrapped in a function, and the function or function call is the return value. A statement is not an expression. It does not yield a value.


#3

put this line "total = 0" before the for loop, and it will work


#4

Yeah I corrected that

Here's the working code


    n = input("?")

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

#5