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


#1

Hi My code is here:

def digit_sum(n):
string = list(str(n))
sum = 0
for n in string:
num = int(n)
sum = sum + num
print sum

numbers = raw_input("Please input you number: ")
digit_sum(numbers)

The error message is this :Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11.
but in fact ,it't can return the right number.


#2

Function primarily can..(when you call/invoke it)

1.prints the output
2.returns the processed-i/p (o/p) using return statement
3.Can do both (1) and (2)

In this case you're printing ,not return-ing

We use return someExpression to return things from functions.

for ex..

def someFunction():
    return someOutput

Point to note:
When we don't return anything by default function returns None


#3

Hi,greentree, thank you.
new code is this:
def digit_sum(n):
string = list(str(n))
sum = 0
for n in string:
num = int(n)
sum = sum + num
return sum
numbers = raw_input("Please input you number:")
print digit_sum(numbers)

and ,greentree ,are you play warIII? I have seen the same ID on Warcraft III Battle Platform.:grin:


#4

:thumbsup: good work! I hope its clear why error occurred !
about WarIII profile? :smiley:
Its not me, I'm not there!:slight_smile:


#5

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