Digit_sum


#1



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


Replace this line with your code. 
def digit_sum(n):
    sum=0
    while int(n)!=0:
        n=int(n/10)
        sum=sum+(n%10)
    return sum

what's wrong with my code?


#2

here:

n=int(n/10)

because you reduce n before adding to sum, the first digit (4) is skipped

you should reduce n after adding to sum


#3

thank you very much stetim94!!


#4

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