# Beyond Frustrated, Suming Digits

#1

I’m not even going to post my code here. How can I break this complicated problem down? I’ve already revisited previous modules and this is still a complicated problem for me. Help me think this through please.

#2

lets say we want to get the sum of 1234 (1 + 2 + 3 + 4 = 10), we need a way to loop over `1234`, given we can’t loop over a integer, how could we solve this?

once we get access to each digit using the loop, what would be a next logic step?

#3

In order to loop over 1234 we need to convert it to a string. We can do this by using the str() function.

str(1234)…

-Remind me why we can’t loop over integers, and only strings?

The next logical step would be to convert them back into a integers in order to sum them together.

#4

not important at the moment, because of how the data type is implemented.

each digit separately, after converting a single digit within the loop back to an integer, how would we get a sum? What constructions could we use here? Maybe a variable where we can add values to?

#5

A number is a singular value, and is not iterable.

``````>>> list(42)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
list(42)
TypeError: 'int' object is not iterable
>>> list(3.14)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
list(3.14)
TypeError: 'float' object is not iterable
>>>
``````

These objects are all iterable…

``````>>> a = {'a': 1, 'b': 2, 'c': 3}     # dict class
>>> len(a)
3
>>> b = [1,1,2,3,5,8,13]             # list class
>>> len(b)
7
>>> c = {8,2,6,9,3,5}                # set class
>>> len(c)
6
>>> d = (a,b,c)                      # tuple class
>>> len(d)
3
>>> e = "some text in a string"      # str class
>>> len(e)
21
>>>
``````

In depth reading…

Built in Types

#6

Could you please walk me through your line of thinking… Codecademy’s modules have been walking me through everything until this point which is why this is so challenging.

I assume you’d initialize a variable we can add values to such as: sum = 0

#7

if we add a variable named `total` and give it a value of zero, each iteration of the loop we could increase `total` with the digit (converted back to integer)

i wouldn’t pick `sum` as a name, because its the name of a built-in function.

#8
``````def digit_sum(x):
total = 0
for i in str(x):
total += int(x)
return total
``````

#9

pretty close. You see the problem? Any idea what it might be?

#10

Not sure where I’m going wrong here

#11

we could add a function call:

``````def digit_sum(x):
total = 0
for i in str(x):
total += int(x)
return total

print digit_sum(434)
``````

then we see the function only returns the first digit. Any ideas how that could be?

#12

total += int(x)

is only adding 434 to total, thus it’s 434 plus total. The correct answer would be 11

#13

but as we can see from the function all, the `total` is `4`

so the loop ends in its first iteration, what could cause this? What do we know about `return`?

#14

I see on the console total is 434.

I know return stops execution of a whole function. Because you call it in a for loop, it executes on the first iteration, meaning it runs once then stops execution

#15

ah okay, i see the problem. I missed a small detail, i was focused on another mistake

here:

``````int(x)
``````

`x` is the integer (434), which is a single value, you should add each digit of sting (casted to integer) to `total`

that is why we have the loop, to get each digit, then `total` is incremented with each digit to get the sum

make the `return` line into a comment and attempt to print each digit (inside the loop)

#16

Still wrong but getting closer…

``````def digit_sum(x):
total = 0
for i in str(x):
print int(i)
total += int(i)
#return total

print digit_sum(434)
``````

#17

The function needs a return value, but how much indentation should there be on that line?

#18

I searched around stack overflow and this is the answer I got.

``````def digit_sum(n):
num_str = str(n)
sum = 0
for i in range(0, len(num_str)):
sum += int(num_str[i])
return sum
``````

#19

Well… That works but it is more complex than needs be. Your code is ample enough to do the task so long as the return line is correctly indented. Compare it to this latest example.

As for using the variable name `sum`, since it’s inside a function it is not hurting anything, but is still not a good practice. We should avoid using function names for variables, just to be on the safe side and not form any risky habits.

``````>>> digit_sum(1234)
10
>>> sum([1,2,3,4])
10
>>>
``````

#20

That good to know that I wasn’t to off, and this is just a more complicated version of doing it…

``````def digit_sum(x):
total = 0
for i in str(x):
print int(i)
total += int(i)
return total

print digit_sum(434)
``````

Lets disregard what I got from stackoverflow then.