Digit_sum


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


Hello everyone, I am having some trouble with my code:


lst = []
result = []
def digit_sum(n):
    ns = str(n)
    for digt in ns:
        lst.append(digt)
    for num in lst:
        x = int(num)
        result.append(x)
    return sum(result)

print digit_sum(1234)

When I input '1234' it prints out '10', which is correct (1+2+3+4 = 10) but then I get this error:

"Oops, try again. Your function fails on digit_sum(434). It returns 41 when it should return 11. "

Then I tried to input '434' to see what's wrong and it prints out '11', which is also correct (4+3+4 = 11) yet I get this error:

"Oops, try again. Your function fails on digit_sum(434). It returns 44 when it should return 11."

although the printed output is correct it still gives me errors.

Thanks in advance.


#2

the exercise calls the function multiply times with different numbers to verify your function works correctly, because these lines:

lst = []
result = []

are not in the function, the list pile up values from the multiply function calls.


#3

Thanks a lot, I scratched my head for awhile on this one.


#4

apart from the exercise validation, its good to have everything you need to get digits sum inside the function, so that in a second python script/file, you can import the function and it works correctly.


#5

Thanks for the advice, awesome community.

first post Btw.


#6