4. digit_sum: Code works but program won't accept!


#1




I FINALLY got the code to work, insofar as it returns the correct answer. However, the tutorial won't let me proceed. I get an error message along the lines of "Your function fails on digit_sum(xyz). It returns None when it should return SUM(x+y+z)" -- but the code DOES return that number!

Any ideas why this is happening? Do I need to change something in the code to have "None" not appear? Or do I need to write different code?.


my_list = ()
def digit_sum(n):
    n = str(n)
    my_list = (n)
    loops = len(n)
    n = int(n)
    # print how many digits have
    print loops
    # set number of digits to be the counter
    counter = loops
    # print the number being analyzed
    print my_list
    # set loop equal to number of digits
    total_numbers = 0
    while counter > 0:
        ccc = int(my_list[counter-1])
        # print out individual digits to check
        print ccc
        # sum the digits
        total_numbers = total_numbers + ccc
        counter -= 1
    print "Total of individual digits is: " + \
    str(total_numbers)
    print total_numbers
digit_sum(434)


#2

No, this code does not return anything. It only prints. Remember, print =/= return.
And usually return will show as None in the console but it doesn't necessarily mean that it is not returning anything.


#3

I added the "return" after the loops and it works now! Thank you!


#4

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