My error message says "Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11." But when I print digit_sum(434) it does print 11, and any other number I test works too. I can't figure out what I've left out or if this is possibly a glitch of some sort. Any help is much appreciated.

def digit_sum(n):
    for n in add_up[0: len(add_up)+1]:
    print sumsum


your function only prints, it doesn't return anything?

Also, the exercise will validate your function works correctly by calling it with different integers as argument. Not the whole script is called.


I have a similar problem


only the function executes, like i already said. How is a going to be an empty list if not the whole script is called?


7 posts were split to a new topic: [solved] digit sum


#Here's the solution by second approach, take care of indentation:

def digit_sum(n):
new_n = n #_(Not needed though, but I wanted to get 1234 printed as well right before returning.)_
length = len(str(n))
new = 0
print "Your number is :",n
while (length >= 1):
num = n%10
print " Seperated number is", num
n = n//10
print " new value to work upon is:", n

    new = new + num
    length = length -1
print"The total of",new_n, "is:", new
return new

digit_sum (1234)


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