# Digit_sum

#1

Hi, can somebody help me? I don't know what I am doing wrong here.
My plan is to convert the integer into a string. Then to split each individual number into a list. Convert each string in the list to integers. Then take the sum of the list.

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

``````def digit_sum(n):
word = str(n)
wordn = list(word)
integers = [int(i) for i in wordn]

print digit_sum(434)``````

#2

Hi @jigpip ,

Your function outputs the result, which was assigned to `answer` ...

``print answer``

... but it does not `return` that result. Lacking a `return` statement, it `return`s `None`.

#3

Hi, I did a more simplistic code, take a look:

def digit_sum(n):
numbers = 0
for i in str(n):
numbers += int(i)
return numbers

#4

Your code only takes the first digit and adds it to numbers. str(n) first has to be converted into a list.
This is how I did it:
integer_list = []
def digit_sum(n):
s = str(n)
lst = list(s)
for inte in lst:
integer_list.append(int(inte))
return sum(integer_list)

#5

Using this code, I get Oops, try again. Your function fails on digit_sum(1000). It returns 12 when it should return 1

#6

if you print the function you'll get 1. Im not sure why the checker doesn't get the same.

#7

This is what I used and I'm honestly not sure I could have done it on my own. I got stuck at the step before making the list.

#8

This is so cool ! It works ......

#9

def digit_sum(n):
chars = str(n)
int_list = []

``````for i in chars:
int_list.append(int(i))
return sum(int_list)``````

digit_sum(1234)

you're welcome.

#10

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