4/15: This should work, but it throws an error: Any help appreciated!


Hi, I have used this code on 4/15:

def digit_sum(number):
number == [1234]
for digit in str(number):
return sum([int(digit)])
print digit_sum(number)

It compiles fine, but then throws this error:
"Your function fails on digit_sum(434). It returns 4 when it should return 11."

I am stumped--what have I done wrong to produce this error? (FYI, if I take out the square brackets in the "return" line, it throws a different error, so I kept all three brackets/parentheses on that line.)


Oh, and I did use the correct indents--not sure why they didn't show up when I typed it.


that is the code I used below.
You are summing the first input from the for function instead of all the numbers.
I recommend you create a variable to store the on going sum.

def digit_sum(n):
number = []
total = 0
for a in str(n):
for x in number:
total += int(x)
return total

You may want to try:

def digit_sum(n):
    return sum((int(x) for x in str(n)))


I am having the same problem as well! When I did test the function with 434, it returned the total 11. But I keep getting the error that it returns none.

Here is my code:

def digit_sum(n):
n = abs(n)
n= str(n)
s= 0
for i in range(len(n)):
print n[i]
s = int(n[i])+s
print int(s)

(this was my test) print digit_sum(434)


Well, you pretty much said it yourself there. Does your function actually return anything?:wink:


it does return the value that it should, but then the system says that there's an error when "434" is tested (even though it works when I do it)


How can your function return anything without a return statement in it? Printing and returning is not the same thing.


LOL thanks for the that! I just realise your first comment was more than a hint! Works now!


this is my code similar to yours but it does not work! would you please help me?

the error is :Oops, try again.
Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "'int' object is not iterable" error.

my code is:

def digit_sum(n):
for x in str(n):
return sum(int(x))


Could somene elaborate on this code? In my understanding it returns the sum of x integrer for this integrer in string(n). But how is x more than one item? And what does n become except a string?


can some one please elaborate this code and what makes you come to this code?


my code compiled fine:
def digit_sum(n):
number = []
total = 0
for a in str(n):
total += int(a)
return total


hi. Thanks for you share.
which part of the code adds list 'a' to number = [ ] ?
does [for a "in str(n)" ] automatically adds the number as string to the number list?
I am a total newbie here.
I will appreciate your kind answer. Thanks.


here is my code
try it

def digit_sum(n):
    a = []
    i = len(str(n))
    while i > 0:
        a.append(n % 10)
        n = n // 10
        i -= 1
    b = sum(a)
    return b
print digit_sum(1234)


Thanks Neimon, this worked and was wayy simpler than what I was trying to accomplish. One thing, Your number list seems to be useless. Your code doesn’t use it at all. No biggie, just thought i’d mention it in case anyone else gets confused.