4/15 digit_sum


OK guys, here was my attempt at 4/15: digit_sum. Here's the code:

to_add = raw_input

def digit_sum(n):

n = str(n)
new_list = list(n)
new_list = map(int, new_list)
sumitup = sum(new_list)
return sumitup

print "Enter your favorite number :" % to_add

print digit_sum(to_add)

And here's the error:

Traceback (most recent call last):
File "python", line 10, in
TypeError: not all arguments converted during string formatting


Hi Farooq

as for your code , to make it work without any errors insert %s in

print "Enter your favorite number : %s" % to_add

But I highly recommend having a look at the following code

def digit_sum(n):
    digit = []
    n = str(n)
    for i in n:
            i = int(i)

    return sum(digit)

posted by BugraBalc here in GitHub


Hi Eisa,

I have implemented a similar approach with how you coded it above:

x_list = []

def digit_sum(x):
#treat x as a list
x_string = str(x)

for i, char in enumerate(x_string):

print x , x_list , sum(x_list)
return sum(x_list)


given the above code, I correctly returned 11 but I have this error:

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

Would you know if this is a possible bug or I just made a real mistake?