Debug Statements

Hi, how can I add debug statements to my Jupiter notebook? It seems I can only edit my functions but print statements don’t work from within them when called by the test cell.

You can add print statements in the notebook.
For instance, print(f'Got x = {x}') in basic_sigmoid(x) prints the value of x.

Yes, the test cells are frequently not editable. But you have a couple of alternatives:

You can do “Cell → Insert Below”, then copy/paste the contents of the test cell into the new cell and then it will be modifiable, so that you can add print statements.

Or you can add print statements directly in your functions. I do that all the time and it works fine for me. Of course depending on where you put them, the output can become pretty voluminous which can make problems for your memory footprint. But if you add prints and they don’t show when you run the relevant test cell then I claim you just aren’t correctly understanding the logic flow. You must be hitting an exception before your print statements got executed. Or something else weird is going on that is not clear from your explanation above.

One thing to note is that sometimes the grader will get upset if you add cells to the notebook. I haven’t figured out the pattern yet, but sometimes it works and sometimes it doesn’t. So you might want to remove any added test cells before you submit to the grader.

Or here’s another theory: just typing new code into a function doesn’t do anything. If you then call that function again, it just runs the old code again. To get the new code actually added, you have to click “Shift-Enter” directly on the function cell (or use some variant of “Cell → Run”) to get the new code interpreted and added to the runtime image. You can easily demonstrate this effect to yourself with a little experiment.