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]
    answer = sum(integers)
    print answer

print digit_sum(434)


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 returns None.


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


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:
return sum(integer_list)


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


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


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.


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


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

for i in chars:
return sum(int_list)


you're welcome.


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