Digit_sum(434) returns None


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)):
    sum1 = 0
    digits = len(s)
    for g in range(0,digits):
        sum1 = sum1 + int(n1[g])
    print (sum1)


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


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


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

you have to return the sum not print it !


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