Digit_sum


#1



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


"Oops, try again. Your function fails on digit_sum(434). It returns 22 when it should return 11."
Python is saying that digit_sum(434) is adding to 22. When I print to interpreter, it shows as 11.


I expected it to return 11.


numberz = []

def digit_sum(n):
    x = str(n)
    for digit in x:
        numberz.append(int(digit))
    print sum(numberz)
    return sum(numberz)
       
digit_sum(434)


#2

place this line:

numberz = []

inside your function, the checking script calls the function only, not the whole script, which means the list doesn't get emptied if this line is not inside the function, which can cause leftovers


#3

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