Digit_sum


#1



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.


numsum=[]
def digit_sum(n):
    add_up=str(n)
    for n in add_up[0: len(add_up)+1]:
        numsum.append(int(n))
        sumsum=sum(numsum)
    print sumsum
digit_sum(434)


#2

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.


#3

I have a similar problem


#4

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


#5

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


#6

#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)


#7

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