Digit_sum code problem HELP!


#1

Here is my code:

def digit_sum(n):
    if n >= 0:
        sum = 0 
        while n:
            sum += n + 10
            n /= 10
        else:
            return sum

I get the error:
Oops, try again. Your function fails on digit_sum(434). It returns 511 when it should return 11.


#2

Hi, @turtwigpo ,

Why are you adding 10 here? ...

sum += n + 10

#3

Uhh not sure. I am very confused.


#4

Ahh wait should I use modulo? It says that...


#5

Try out modulo, but also consider why you would use it there.


#6

Thanks it works now :grin:


#7

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