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


#1

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


#2

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


#3

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):
number.append(a)
for x in number:
total += int(x)
return total


#4
You may want to try:

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

#5

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)


#6

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


#7

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)


#8

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


#9

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


#10

hello,
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))


#11

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?


#12

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


#13

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


#14

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.


#15

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)

#16

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.