Beyond Frustrated, Suming Digits


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.


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?


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


-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.


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?


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

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

These objects are all iterable…

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

In depth reading…

Built in Types


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


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.

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


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


Not sure where I’m going wrong here


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?


total += int(x)

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


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?


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


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



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)


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)


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


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


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)
>>> sum([1,2,3,4])


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.