Digit_sum - I'm noy sure


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


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
#2

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

as

total += char

#3

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


#4

Yes. It is.

I tested the function with

print digit_sum(235)

and rightly got 10


#5

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