Why string?


#1

def digit_sum(n):
total=0
sum_list=list(str(n))
for digit in sum_list:
total=int(digit)+total
return total

so guys im trying to understand this here, why do we have to convert the "n" to a string?
I understand why we have to convert to a list.


#2

We can use an arithmetic algorithm that implements modulo and integer division, or we can use the list method. A number cannot be converted to a list directly, so we convert it to a string, first. A string can be split into a list.

Now that we have a list, we need to evaluate each digit as an integer, after which we perform the addition operation.


#3

You don't have to do anything other than line up a series of instructions for the computer to do produce the correct result. There are several ways to get there.

Why do you think list is needed? Wouldn't a string do the same thing?

And how would an integer get converted to list? An integer doesn't represent a collection of values


#4

Right you are, @ionatan. A list is not really needed, here.


#5

thanks so much, that makes so much sense now!!