4. Digit_sum



I get this error:
Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11.

How can I get rid of those "none"?

def digit_sum(n):
    x = str(n)
    y = int(len(x)) - 1
    total = int(x[y])
    for num in range(y):
        if y >= 0:
            y-= 1
            total += int(x[y])
    print total

print digit_sum(434) 

I get this in console:



You're printing the result of calling your function, which as the error message mentions is None
So if the result is None and you print the result, then it should be no surprise that there's None in the output

Likewise, executing print statements is going to result in multiple values being printed

Also note that you've got duplicate code for controlling the number of iterations


Try changing your "print" inside the function for a "return". And lose the "print" before calling the function.


lst = []
def digit_sum(n):
for i in str(n):
print lst
return sum(lst)

Why this solution is not work on Codecademy?
But it works on PyCharm.


Yes, I already got it right, but thanks for help :slight_smile:


you can directly just change it to a string and then append it as an integer, at the end you can return a sum of the function, just like below

def digit_sum(n):
Alist = []
x = str(n)
for i in x:
return sum(Alist)


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