Digit_sum 4/15 error



This is what I put during the "testing phase" to see what will/would happen. Essentially doing a step process to confirm each line of code is functioning correctly and doing what I want it to do. (before doing anything fancy.)

test = 12345
test = str(test)
for x in test:
    test1 = int(x)
    stupidcode = stupidcode + 1
    print "total", stupidcode
    print "integer", test1
    print x

As you can see by the "stupidcode" designator; I have a problem! The short of it is that for whatever reason, every time I hit the submit this code function; the stored information for said variable isn't resetting. The integers are printing correctly. The addition is adding but... every time I hit the submit button, it keeps adding from previous.

total 46
integer 1

So after a few rounds, I'm up to a total of 46. (I regressed the code to the point where I only have 1 variable. stupidcode = stupidcode + test1 has the same problem.)

1. Is this an error with the codecademy website? I have indeed changed the variable name a few times.
2. How do I clear the stored variable paramater? I don't want to put a test1 = 0 at the start.

Also, I'll end up making a def digit_sum(x): afterwards.


We are asked to write a function, digit_sum() that returns the sum of the digits in the parameter.

def digit_sum:

    return total

To be able to accumulate a total, we need to define a starting value. This is called initialization.

total = 0

Please don't insult our intelligence by using juvenile language in your code. Naming convention is easy to follow, and something your readers will appreciate.


Just to clarify, I'm not calling your code stupid. I'm calling my code stupid; as it isn't doing what it's supposed to do. I'll choose whatever reference I want to reference my code as when troubleshooting something that shouldn't show up.

I've taken a few different courses and initialization doesn't have to have a supplied value. You can leave it null; or you can call it in a number of different ways inclusive of:

total = total +1
total += 1

among a few other ways on your first initialization. You can even initialize as:
total = total2

Initialization isn't my question. That's simple.

Again, I've already stated very specifically that I'm running each step independently first prior to writing the accepted requirements of some class segments like this one. I want to know what each step is doing and if each step is doing what it's supposed to be doing independently from a more complex option; comparatively to a couple other beginner courses I've taken in C, C#, Basic, etc. Hence why it is broken down into two separate segments with print statements to see if there is a problem.

Please yet again take note.

The initialization isn't an issue. It's that it's retaining the data from the previous time I ran the program. It shouldn't retain data. It shouldn't already possess the variable I called within the previous iteration of the program; especially if it's housed within a for statement. Which is what I'm very much curious about. Hence the two questions. The first one is simply asking if this is a limitation of the Codecademy system? If so, I can continue on my way as it isn't a problem I can fix.

For question 2 I very specifically asked is there a way to not have to initialize without using total = 0. The reason for this is that I don't want to have any variables remaining in the system. When I shut a program down, it shouldn't retain this information. It shouldn't retain the variable name. It shouldn't retain a quantity within the variable field. On all previous attempts at breaking down each segment for the function I have not had to do this. I could hit the "submit" my 10-20 times and not have the information retained. I could not call the previous quantity total after I hit the "submit code" button the second time after I ran the program as total12. If I wanted to simply "make sure" the quantity in the variable isn't retrievable, the simplest way is to give it a different value. I don't want that. I don't want the variable itself to be called again. Currently to me, it looks like I could run a separate program at anytime after I use this one and then retrieve the data. That isn't a safe-secure way to code. Since I don't know python, I was hoping there was a call function that would actually flush this information.

This is very much the only exercise that I've come to which is performing the retention.

edit: I forgot to clarify that the variable was in a for statement.


>>> c += 1
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    c += 1
NameError: name 'c' is not defined

c is not initialized so cannot be accumulated.

for blocks are in the same scope as the for statement.

total = 0
for x in test:
    total += int(x)
print total            # 15

Given that this is all inside a function block, the variables are garbage collected on exit. Nothing is retained. In your globally scoped code above, the accumulated value is never reset to zero and is still resident in the environment's memory. Run and Submit do not reset memory. Only a page refresh will do that.


Thanks. Now I know why it's staying retained. Much appreciated.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.