4/15 Practice makes perfect digit_sum


#1
digs = []
def digit_sum(x):
	num = str(x)
        for char in num:
          digs.append(int(char))
        lol = 0
        for z in digs:
                lol = lol + z
        return(lol)

I don’t know why this is giving me so much trouble, but I can’t figure this out. I keep getting the error

Your function fails on digit_sum(1000). It returns 12 when it should return 1.

and I can’t even begin to figure out what is doing that. Help me please?


#2

Don’t mix tabs and spaces :confused:

As for where you got 12 from, a good start is to add prints to follow what’s being done to reach that number.
Also observe what happens when your function is called multiple times, is there any way your previous calls may be affecting later results?


#3

As far as I know I haven’t been mixing anything. I use tab to indent. The program does not always like this though. I’ve spent a lot of my time trying to get the thing to accept my spacing and having to backspace it or extra tab it around to make it work.

I had it print out the list digs right before it hits the lol variable. the results it shows for an input of 1000 is

[0]
[0, 4, 3, 4]
[0, 4, 3, 4, 1, 0, 0, 0]

So apparently it’s adding the numbers up right. The problem seems to lie in the appending of digits somehow? I am so very confused on this part though.


#4

There’s not much to indentation.

Functions, loops, classes etc have some kind of body. You indent that by 4 spaces, and deindent by 4 spaces to exit that body.

A tab character is interpreted as “go to the next multiple of 8 spaces”

Yes, your program remembers all digits it has ever seen. This would not happen if you started out with an empty list for each number, so what are you doing differently from that?


#6

That’s what I’ve been trying to do. I don’t have a problem in other python programs. It’s just the codecademy one that doesn’t like what I’m doing for some reason. I hit tab, it only moves 2 spaces which I know is wrong but I’m not sure how to make it work. So I double tab to get 4, and the program gives me indent errors. It took me at least 10 minutes of back and forth to make the tabs work in a way the program would accept. I had them all at 4 spaces (2 tabs) and it kept throwing indent errors until I got the indents as you see above. And every time I add something the indents are no longer right so it’s another 15 minutes of trying to make it work. It’s been incredibly frustrating tbh.

I don’t understand what you’re asking either. :confused: I don’t think I have any other numbers there. The first for loops should just put whatever number was entered as individual strings into the list. The second one should add the numbers in the list together.

I screwed around with it just now, couldn’t figure it out, copied and pasted from above, and now it’s working when I print it out. I literally did not change a thing.


#7

You either changed the code or you’re using it differently. There is something wrong about the behaviour in the code.

The problem arises when you call it multiple times:

digit_sum(234)  # 9
digit_sum(234)  # 18
digit_sum(234)  # 27

And this isn’t too difficult to debug, it’s merely a matter of looking at where you are reading those numbers from, reasoning about why you would have no numbers at the start of the function.

There is a tab character in that code, that is what is giving you grief. Yes, this is mostly an editor issue, an editor should help get this right, not the opposite.


#9
digs = []
def digit_sum(x):
	num = str(x)
        for char in num:
          digs.append(int(char))
        lol = 0
        for z in digs:
                lol = lol + z
        return(lol)
      
print(digit_sum(104))

That is what I used that worked. As far as I can tell the only difference is the addition of the print at the end. I’m not sure what you mean by a tab character in the code though?

I did try running more than one and I can see that problem of multiple adding. I am not quite sure how to fix that. I don’t think it’s happened in previous lessons. I will go back and try to figure that out at a later time I think. Aside from some very basic HTML I’ve never coded before so it’s a steep learning curve already.

Also thank you for helping. It’s much appreciated!


#10
'	'

^ tab character (highlight the text with your cursor)
There’s one of those in your code.

Right now you only have one call to your function. The bug only happens for multiple calls.
If you add more calls at the bottom, you can tell that this code does not work in the sense that it does not accurately produce the digit sum each time it is called.

And then you’d start looking at where they are coming from. There’s nothing subtle going on here. Your code very much says to keep numbers between calls.


#11

Oh, that makes sense! I started just typing out spaces manually and that seems to be working to fix the indent errors at least. Huge reduction of frustration just from that lol.

I think what I’d need to do is add a command after the return(lol) to tell it to make the digs list empty again, right?


#12

No, rather, you shouldn’t be keeping the list at all, it should get entirely discarded, not emptied.

A list should get created when your function runs, because your function needs a temporary list. After the function has finished, there’s no reason for that list to continue to exist.

The only place where that list should even be accessible, is inside that function. Nothing else has reason to share it


#13

Very rarely should anything be shared in code. There is little to no benefit, and it opens up for the possibility to cause interference between unrelated things, as you discovered.

Imagine if you ran the function twice at the same time. There would be two functions currently executing, but only one list.


#14

Oh so move the list inside the function. That makes a lot of sense now. :sheep:

Thanks so much for taking the time to help me out!