Python Summing Numbers


#1

Please explain this to me like I’m in the fifth grade. I’m trying to break this down on my own and the instructions are confusing as to how they came up with this solution.

How does this work exactly? Why do we define the total? Why did we go with a while loop? x % 10… Why? Floor division… Why?
Please explain this as simply as possible.

def digit_sum(x):
total = 0
while x > 0:
total += x % 10
x = x // 10
print x
return total


#2

you miss out on valuable learning opportunity by using get solution, you should really code the solutions yourself.

lets say we want to get the digit sum of 1234, we can’t do this in a single go, we need to first add 1, then 2, then 3 and finally 4, which is why we have total:

total = 0
total = total + 1
total = total + 2
total = total + 3
total = total + 4

we reassign total, and by including the current value stored in total, total will increase (to get sum of all digits)

using x % 10 and x // 10 isn’t the simple solution, try casting x to a string so we can iterate over x, then add all the digits to total (don’t forget to cast back to integer)


#3

Thanks for the feedback. I’ll work out the solutions myself in order to get those valuable learning experiences.

I’ll reply after I the simple solution.


#4

I’m hoping to go back forth here in order to learn more.

I’m currently in the process of converting the integer n into a string. How come n is considered an integer in the first place? Isn’t it just a string already?

How do I select the appropriate loop to begin iterating?


#5

you mean converting x (which contains integers) to string?

The parameter (x) gets its value from arguments at the function call, which the exercise tells you will be integer. That is the task, with a task come specifications. You can also add your function call for testing purposes.

consider what possible loops you have, then consider how these different loops work for strings, then apply the best one


#6

Yes I mean x. Coding is great, I enjoy this challenge. Appreciate your help. I’ll let you know how it goes.


#7

Is there a general rule I should be aware about when considering using a for loop or while loop?


#8

isn’t that already explained somewhere in the course, anyway, here is recap:

https://stackoverflow.com/questions/920645/when-to-use-while-or-for-in-python

we know how many iterations we want to make (for each character in string), so then you use a for loop.


#9

At this point I believe it would be beneficial to see the code myself… Can you show me how you’d construct the for loop?


#10

Wait, how did you get to this point in the course and being unable to make a for loop?

python has official documentation with examples:

https://docs.python.org/2/tutorial/controlflow.html#for-statements

as well as plenty other sites which you can consult


#11

I know a for loop is constructed this way:

Example = [dogs, cats, pets]

for x in example:
print x

Yet this problem doesn’t utilize lists, or variables thus what am I iterating over?

def digital_sum(x):
for str(x) in ??


#12

you are looping over str(x), which you could store in a variable first if you want


#13

Can you please show me how that code would look?


#14

instead of the list (example), you loop over str(x), why is this so difficult to grasp?


#15

Thus…

def digit_sum(x):
x = str(x)
for str(x) in x:

?


#16

if you cast to string first, you can just loop over x:

x = str(x)
for digit in x:

#18

str() is the built-in function which cast the argument to string, so then you get: str(x)


#19

Typo. My bad. Is this correct?

def digit_sum(x):
  n = str(x)
  for digit in x:
    n = str(x) + int(x)
    print x

Appreciate your feedback thus far


#20

what are you attempting here:

n = str(x) + int(x)

could you explain it to me? So i can see where you go wrong


#21

To be completely honest, this was a guess as to how I can sum all the integers together.

I’m not entirely sure how to add ‘123’ together after looping.

All I know is that it must be done.