Jupyter Notebook problem (regarding loops)

The problem is about rerunning loops.

For example this loop:
(from a codecademy project)

bmis = [‘31.1’, ‘22.4’, ‘25.3’, ‘20.6’, ‘22.1’, ‘26.9’, ‘29.3’, ‘22.7’, ‘33.1’, ‘24.8’]
total_bmi=0
for bmi in bmis:
total_bmi += float(bmi)
print(total_bmi)

If this is run on Codecademy it will always say 258.3
If this is run on Jupyter Notebook in ONE CELL it is also always going to say 258.3

BUT (and here comes the incredibly huge BUT) if this is written in two seperate cells as:
(cell1:)
bmis = [‘31.1’, ‘22.4’, ‘25.3’, ‘20.6’, ‘22.1’, ‘26.9’, ‘29.3’, ‘22.7’, ‘33.1’, ‘24.8’]
total_bmi=0
(cell2:)
for bmi in bmis:
total_bmi += float(bmi)
print(total_bmi)

Then every single time you run the second cell the kernel is going to add itself to that and again and again and again (285.3, 516.6, 774.9… etc), which is incredibly annoying. What is even worse is that it will keep calculating with that new sum… (It does not matter if these are strings either, it is going to duplicate all the strings over and over again every time you run it)
And the only way to fix it is to reset the kernel and run all the cells.

So my question is:
Is there a way I can block this very very annoying and useless malfunction?

I don’t know if there’s a way to keep it from printing again,
but if you want the sum to reset to 0 each time the the second cell runs,
then move total_bmi=0 to the beginning of the second cell.

1 Like

Also… I get why you broke up the code like that–for readability(?). But, you can put whatever code you want in each cell; you don’t have to break it up like that–just put all that code that you posted above in one cell.

Every time you run each kernel, the executed code is stored in memory.
https://www.tdcm.io/post/jupyter-notebook

https://www.edlitera.com/blog/posts/guide-how-to-use-jupyter-notebooks#mcetoc_1g40bka8v6n

I broke it up like that as the codecademy projects expect me to break up like so. After every little task there is a place where I guess they expect me to enter code and very often these tasks are that very short, leading to mistakes in Jupyter Notebook as yes, the kernel is going to save the value.
But still I do not understand what is the use of such thing? Rerunning a cell and having it multiply its data. If I wanted to do that I would write a code for that. It is really an annoying thing of Jupyter Notebook.

But I guess then the only way to deal with it is to ignore the way Codecademy projects expect me to do a bit-sized code into each cell and just do it all in one.

Well, you wouldn’t separate the code like one would in the CC LE. The LE in the CC terminal is coded differently (in the background) than Jupyter.
You have a variable definition which is a list of bmi numbers, then you have a counter set to 0, then your for loop logic. All of that would be in one cell in Jupyter b/c it all goes together. If you separate the code like you originally had it, the for loop is going to loop through the list every time that code cell is run and add the total onto the counter. You can resolve it like Jan said above. Or, just have that code all in one cell.

If you have a lot of code sections, sometimes it’s easier to de-bug if the code is broken up in different cells. Then you can run the cells to pinpoint the section where the error lies.

1 Like