C5_W3_A1 Ex. 2

What that probably means is that you had changed one of the cells, but had not executed it by actually running that cell. If you type new code into a function cell and then call that function without actually running that cell, it simply runs the old code. You have to actually click “Shift-Enter” on the changed cell or use the “Cell → Run” menu to get the new changed code added to the runtime image. Simply calling the function again runs the old code. Doing “Kernel → Restart” is one way to force things back into a consistent state, since you then need to run all the cells again. The other way would just be to click “Cell → Run All”.

You can easily demonstrate this phenomenon to yourself. Pick a function that you know is correct. Then purposely break it: multiply one of the output values by 42 before the return statement. But DO NOT click “Shift-Enter” on the cell. Now run the test cell that checks that function and it still works, right? Then click “Shift-Enter” on the newly broken cell and then run the test cell again. Kaboom!

So the point is that you always need to be conscious of the current runtime state of the notebook. Doing “Kernel → Restart and Clear Output” followed by “Cell → Run All” or “Cell → Run All Above” is one good way to get everything back into a consistent state.

2 Likes