The code works, but I get an error message!


#1



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


I get this error message:
"Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11."


But both the terminal and Spyder print 11 to digit_sum(434)! What's wrong with my code? Is this a bug?


def digit_sum(n):
    total = 0
    for digit in str(n):
        digit = int(digit)
        total += digit
    print total
    
digit_sum(1234)
digit_sum(1)
digit_sum(434)
digit_sum(87400)


#2

Oh, I figured it out. I had to return it instead of printing. Sorry, guys!


#3

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