4/15 - I feel so close!


I put in comments to demonstrate my thinking. What’s the flaw in my thinking?

digit_total = 0

def sum_digits(n):
  #Defined function, should be good
  for i in len(str(n)):
  #len(str(n)) should return 4, if n = 1234
    digit_total += n % 10
    #Should store 4 with sum_digits(1234) before iterating
    n // 10
    #Gives me a new n removing rightmost digit
    #Loops for n-1 more iterations

(I get an int is not iterable error.)


length is an int and you’re trying to loop through it. You should have a line number in that error message telling you as much.

Missing some other stuff too, but the idea is sound


Can I get a little push?

Should I turn the input into a string first, then turn it back into an int and do the math on it? Wait that doesn’t make sense…

If I use

  for i in str(n):
    total += int(n)

Shouldn’t that iterate through every digit in the number and turn each digit into an integer to be added to total?


Probably not, but you can keep asking.

Yeah, you’ll get the right amount of iterations that way. I would’t do it that way because generating the string actually involves identifying the digits (which is what you’re trying to do). Totally possible but you’re using the solution to make the solution.
When should the loop stop, what other tells are there that you’re done?

I don’t object to using strings to get the sum, but if doing that it would be by letting the string conversion do all the work. Your approach is more low-level so it ought to use simple things (just math operations)

…I totally botched that explanation, not sure how to express that in a good way.

If you implement addition, then it would be cheating to use subtraction to do it for you. (by double-negating)
But it would be clever to implement addition through subtraction, if that’s the intention.


Oh right. You totally dropped the math approach. (Didn’t even read past the loop)
Yeah, that’d do it.

And, while it’s “cheating” in terms of computer science, it’s a better match for how humans would do it, because we can read one digit at a time, just like from a string, in fact, we represent them as strings in our minds (unless thinking about them as volume or size etc) and on paper

Computers totally lack that operation, it has to be built with other things (division)


It doesn’t do it!

The error says that it returns None when it should return 11 when trying digit_sum(434)

But I don’t see why it should return none because hear me out! I’ll even go through it here…

def sum_digits(n):
  digit_total = 0
  for i in str(n):
    digit_total += int(n)
    print n

Let’s begin: When calling digit_sum(434)…

  for i in str(n):

This should give me 4 for my first iteration as a string.

    digit_total += int(n)
    print n

I add 4 as an integer to digit_total, then print it to see whats up.

I should add 3, then 4 to the total after that. What gives? Ahhh… this is stressful haha


It’s like we’re in a sandbox

Yeah it does! …conceptually speaking anyway

But there’s of course a bit of extra code that needs to go around it. Also, you’re not converting each digit, you’re converting all of n


I should return digit_total at the end I suppose. And then all that’s left is to convert each digit and not all of n, huh…

Wait. Even if I convert all of n, shouldn’t it iterate through each “letter” in 434 still?


You’ll end up with 434 * 3
434 + 434 +434

int(434) -> 434


But… But but but but…

for i in str(2345):
  print int(i)

Gets me this


I used the above as a template to try and take each digit of n using the same pattern:

for i in str(n):
    digit_total += int(n)
    print n


If it was the same you’d get the same


I know. That’s why I’m confused and posting here… =(


Time to start comparing. It’s 2-3 lines right. gogo.


is int(n[i]) jibberish? haha…

Im onto something right?


Does the one which produces the digits do that?

You have a version that does that you want already. compare. You can even switch things out to go from one to the other


OH MY GOD where have I been looking all these hours?!

I feel so dumb now


Oy, that was only like 10 minutes, you were doing division before that :confused:



It returns None when it should return 11.

I thought I had it, but I didn’t. Time to start looking at the 2-3 lines again. =(


At which point you should ask what you do/should/where return


I’m storing the totals into digit_total, so it makes sense that I return digit_total which represents the sum of all the digits iterated… argh hmm…