Digit_sum code passes but is it correct?


#1

Hey all - this is the code that I wrote that works, but it is different than most of the solutions I see here. Is this acceptable or is there a better way? If there's a better way, please provide a why so I can have a greater understanding of Python. Thanks!

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

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

#2

This is very clean and short in my opinion. You should know that what matters is for your code to do the needful (pass the test cases) and you're good to go.

Cheers! Have fun coding.


#3

Thanks for the reply. Glad to know I'm starting to get it


#4

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