# 4. Digit_sum

#1

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:

11
None
None``````

#2

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

#3

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

#4

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

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

#5

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

#6

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:
Alist.append(int(i))
return sum(Alist)

#7

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