Digit sum


#1



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


the code works great and prints the correct sum of the numbers, however Codecademy will not accept my code! help much appreciated :slight_smile:

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


The prompt is:
Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits.
For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4.
(Assume that the number you are given will always be positive.)
Check the hint if you need help!


def digit_sum(n):
    num_list = []
    n = str(n)
    for i in n:
        num_list.append(int(i))
    print sum(num_list)
        
        
digit_sum(12345)


#2

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

print sum(num_list)

You're printing but you had to return ?

why return why not print worked?
Answer is inside instruction.

Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits.


#3

:joy: :joy: :joy:

thanks, return worked


#5