The difference


I'm not stuck, again, but I want to know some specific things.

It said "input value of 434 returns 434 instead of 11"

Could you tell me why it didn't work out??

This is the code that worked out.
def digit_sum(n): 
    if n > 0:
        nums = str(n)
        nums_sum = 0
        for i in range(len(nums)):
            nums_sum += int(nums[i])
        return nums_sum

This is the code that did not.
def digit_sum(n): 
    if n > 0:
        nums = str(n)
        nums_list = nums.split()
        nums_sum = 0
        for i in range(len(nums_list)):
            nums_sum += int(nums_list[i])
        return nums_sum


Hi, @programmer_wannabe .

Make sure you understand what the split method returns.

See the documentation at Python: str.split.


For example, ' 1 2 3 '.split() returns ['1', '2', '3'] ??
This is legit.


Yes, but your example str contains whitespace, which is a default separator.

Using interactive mode in a Python interpreter, let's try a str with no whitespace, which is what we get if we convert an int directly to a str ...


There was no whitespace, so we wound up with the entire str in a list.

However, note this ...

>>> list('434')
['4', '3', '4']

Now, you can reconsider this line ...

nums_list = nums.split()


You are right. Thank you very much. <3