4. digit_sum fails on 434



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)


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


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

What exactly does this line mean?



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

digits = [ ]

for d in str(n):

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

Hope this helps.


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