Did get the digit_sum part right but need help understanding it


So this was my code for the digit_sum part of the section and quite frankly, the section did give me a lot of issues of all the sections of Python. Even though I got the part right, I still need some help understanding them so I am going to ask you guys some questions.

def digit_sum(n):
   total = 0
   nstring = str(n)
   for digit_sum in nstring:
       total = total + int(digit_sum)
   return total
print digit_sum (314)
  1. We set our total to zero, why did we do that?

  2. I know that the key to the whole problem was the "total = total + int(digit_sum)" part but I don't get how it is supposed to give us the sum of a number's digits. Can anyone please help explain to me how it would work in regards to adding up the digits of 314?


<rant>I've never really been able to figure out how a person can write a piece of working code and then not understand how it works. Just baffles me. Not making any accusations but this is usually the case when somebody copies code to pass, rather than write it themselves for the sake of accomplishment and real learning.</rant>

To initialize the variable with a value of zero. The program accumulates to this variable.

The value in the parameter is a number which has been explicitly converted to a string object. The for loop iterates over the string one character at a time. I would have chosen a different variable name for the iterator, but no matter.

The character is then explicitly converted back to an integer which may be added to total. Once the loop is complete, all the characters have been made into numbers and added together. The total is then returned.


We initialize the variable to zero so that we can make sure that the value is '0'. It sounds redundant bu t if you use only within the the loop you might get an error.

2 - Remember that the program is supposed to recieve a variable, however, if you want it to split you either need to convert the variable into a string and then for each character in that string, you will convert it to an int and then add it to total, and so on until the loop ends.

You could also try the other suggestion and see what becomes of it.


For some of them I did get to the point where I was copying and pasting code, very bad habit I know but I am doing my best to fix it.


It's not a bad idea to consult another's person code, it can be very enlightening.

However, it must not be your first choice, try coding your answer first and then, after many tries, consult an answer. You will improve a lot faster.


Study code. Work out what it does. Learn how to recognize patterns and purposes. Looking at other people's code is most beneficial if the take away is a new understanding of the concept we're working to learn. In that sense, we need to study other code samples in order to put the pieces together in our own mind. At some point though, the onus will be on us to create a solution from scratch when given a problem.

Here's to rising to the challenge. Happy coding!


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