Digit_sum(434) returns None


#1



Error message : Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11.

Made this code in PySripter. Digit_sum(434) works fine there.
Can't really understand what's the reason behind this problem.

def digit_sum(n):
    s = str(n)
    lenght = len(s)
    n1 = []
    for z in range(0,len(s)):
        n1.append(s[z])
    sum1 = 0
    digits = len(s)
    for g in range(0,digits):
        sum1 = sum1 + int(n1[g])
    print (sum1)

#2

On the last line you print sum1, you don't actually return it.


#3

@ruslanis ,

The final statement in your function is ...

print (sum1)

It displays a result, but does not return it. Without a return statement, your function returns None.


#4

def digit_sum(n):
s = str(n)
l1 = len(s)
sum = 0
list1 = []
for i in range(0,l1):
list1.append(s[i])
print list1
for g in range(0,len(s)):
sum = sum + int(list1[g])
return sum
digit_sum(434)

you have to return the sum not print it !


#5

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