Digit_sum


#1

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

File "python", line 6
digit_sum(1234)
^
SyntaxError: invalid syntax

I think that syntax is well, help me pls


#2

return is final door to get off the function's body,Your function will iterate one time and will get off the functions body.

str(n[i:i]) (you also missed ) there )will return empty string at each execution ,so even you convert it to int type,your casting will fail.

one way is to...
just iterate over string form of n (str(n) )and add iteration variabele i by casting it into an int type to variable s at each iteration.
then return the s.


#3

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