4/15 digit_sum



Every input into digit_sum(n) prints 3

Replace this line with your code. 
def digit_sum(n):
    digit = len(str(n))
    total = 0
    for i in range(digit):
        total = total + int(str(i))
    return total

print digit_sum(123)


try using an if/while/else statement instead of a loop


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

print digit_sum(1)

got it


Good job :grin: :grinning:


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