Digit_sum - I'm noy sure



I've done the exercise and codecademy says it's good done, but I have bad feeling about this.
Can someone check it?

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

Digit_sum: converting string to int help

This should work like a charm. Except you want to further minify this line:


total += char


Yeah I know about this. I'mmostly using it. But the rest is ok?


Yes. It is.

I tested the function with

print digit_sum(235)

and rightly got 10


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