def digit_sum(n):
    n = str(n)
    n = int(n)
    return n

Oops, try again. Your function fails on digit_sum(434). It returns 434 when it should return 11.

I don't know how to do this problem.


declare a variable in which you store the sum/total

loop over a string version of n, and add each item to total, don't forget to cast to integer


I don't know how to loop over a string version of n though....


You'd convert the number to string and then iterate through the characters of the string
You should have come across those things by now. Just the same you can google those actions if you don't remember how.

You can also isolate the digits using multiplication/division/subtraction, something you can think about while you're waiting for the buss or something


Thank you!!! That really helped me a whole lot!


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