digit_sum


#1

def digit_sum(n):
print "we are in"
total = 0
for digit in str(n):
total += int(digit)
return "why it does return None"

Prints out "Your function fails on digit_sum(434). It returns None when it should return 11."

I dont get the problem. Can someone help?


#2

Why do you return a statement instead of solution? Your code fails because your function doesn't return correct value


#3

Hi,

you have to indent the function body.

def my_func:
    print "this line starts with 4 spaces"

I think they changed the function to return a string literal after reading the error to make sure it returns something.


#4

It is indented, just copied like this


#5

I wanted to see if it does return anything

And i fixed it with:
def digit_sum(n):total = 0
for digit in str(n):
total = total + int(digit)
return total


#6

So does it work now?


#7

You can leave

it is okey.
I've tested your code and all I had to do was to change

onto return total

Also, to present your code properly indented use three backquotes before and three backquotes after your code.
Backqoute is this


#8

Yep, you where right :slightly_smiling: I was little confused by that.


#9

Awesome thanks :smiley:


#10

Another simple alternative.