4. digit_sum fails on 434


#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 None when it should return 11.


It should return 11 and let me move on to the next challenge. It returns 11, but tells me it returns None...after the console just displayed 11!!!


def digit_sum(n):
    digits = [int(d) for d in str(n)]
    b = sum(digits)
    print(b)
    
digit_sum(434)


#2

That's because you aren't returning anything. You're only printing the result. print and return are two different things.


#3

Can you please explain how you got to:
digits = [int(d) for d in str(n)]

What exactly does this line mean?

Thanks!


#4

This is a list comprehension, a quick way to iterate a list. It would be
written otherwise:

digits = [ ]

for d in str(n):
digits.append(int(d))

I used the int() to turn each character in the string into an integer.

Hope this helps.


#5

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