Dark theme

Extras


A few additional things you can try.

You may have noticed that you can set a "conditional" breakpoint under the Debug menu. Such breakpoints are only active where the condition is True. Delete the current breakpoint, and try adding a conditional breakpoint with the condition i == 3. You should see that it stops only when i is three. You can edit conditions by clicking on them in the "Breakpoints" pane, which appears in the same place as the Variable Explorer. If it isn't there, you can show this using the menu under View --> Panes.

You can actually step through the code a line at a time, if you want to. This is not so useful for sizable code, but for a few lines it can be helpful. To do this, clear all the breakpoints (a menu item for this is under the Debugging menu). Push the debugging button to start debugging. You should see that the first line in the code has a subtle highlighting. Push the "Step" button next to the debugging button (it has a curved downward arrow and a dot on it), and you'll see the highlighting move down to the next line. This has executed the last line it was on and moved to the next line. You should see variables being produced in the Variable Explorer. To step through the lines using the keyboard and the prompt, press s and then ENTER.

Note that if you do this one line at a time stepping, you'll see functions run when they are called, but the execution will just do these without stepping into the code block and running the function a line at a time. Here's a file you can use to test this: test2.py. If you want to "step into" the functions and run those a line at a time, when you get to an appropriate line with a function call highlighted, instead of pushing the "Step" button, push the "Step into function" button (Blue bent arrow pointing between two dots). You can also run down to the bottom of a function and halt with the button next to it (Blue bent arrow pointing out from two dots).

You can also change the value of variables as the program runs. This is obviously easy to get into trouble with (altering one variable while not keeping other variables in tune is likely to break a program) but it is occasionally useful. To do this, simply type the variable assignment as it would be in the code, at the debug prompt:
ipdb> data_floats = [1.0, 20.0, 300.0]
You should see the value change in the Variable Explorer.


So, given all that, here's a file to tackle: test3.py. Run it in Spyder and read it through. It clearly doesn't do what it is meant to. Can you, using the debugger, work out why? It's a tricky one, so there's a hint on the next page.