CSC 226

CSC 226 logo

Software Design and Implementation


T7: Debugging

Paired assignment T7 should be completed with a partner.

One of the most common frustrations with programming is finding bugs and fixing your code. Debugging is the process of identifying and eliminating unexpected behavior produced by your code. The IDE understands your frustrations, and has tools to help make debugging code easier for you. Let's first explore some code, then look at some of these tools.

Here are a few of the most important tools you'll need to understand to become a master debugger. First, you will need to set the debug mode of the IDE as follows:

Spyder Debug Controls

PyCharm Debug Controls

spyder debug
Figure 1Ss: Spyder Debug Shortcut Menu
or

spyder debug
Figure 1Sm: Spyder Debug Menu


The Spyder Debug button
: The very first control in both the debug menu and shortcut debug menu turns on the Debug mode Spyder debug You can also use Contol-F5, which will run your code in debug mode rather than simply running your program directly.
debug in PyCharm
Figure 1P: PyCharm Debug Menu

The PyCharm debug button: Found under the "Run" menu option. The debug button will turn on debug mode to run your code in debug mode rather than simply running your program directly.


Breakpoints: Debug mode will make it so that it will stop at your breakpoints. Clicking on the button again after reaching a breakpoint will resume running your program until it ends or another breakpoint is reached. Breakpoints are lines in your program that you have indicated where the IDE should stop running the code so that you can see a "snapshot" of what the variable values are on that line. Figure 2 below shows an example of setting a breakpoint INSIDE a function, which is very useful.

In most IDEs you can set a breatkpoint, highlight the line where you want to set the breakpoint and click in the margin. A red dot will appear as shown in Figure 2 below. Note that busing nreakpoints before returns (as shown) is generally a good idea.
    A breakpoint in Spyder
    Figure 2S: A breakpoint in Spyder
    A breakpoint in PyCharm
    Figure 2P: A breakpoint in PyCharm

    The debugger console: Most debuggers gives you a list of all the variables that are defined and have value in the program. Of course the IDE cannot display a variable UNTIL it is actually used. Also take note that your normal console (where syntax error messages typically appear) may now be hidden behind the debugger tab. Spyder variable explorer
    Figure 3S: The Variable Explorer in Spyder in debug mode

    The debugger console in PyCharm

    Figure 3Pa: The debugger console in PyCharm during debug mode

    The PyCharm Watches tab: PyCharm has a feature which allows you to select which variable you want to keep track of, rather than all the variables. In Figure 5, you can see Watches is monitoring the variable playAgain.Note that this is not available in Spyder, but the Variable Explorer is a nice alternative.

    The Watches tab in PyCharm's debugger console

    Figure 3Pb: The Watches tab in PyCharm's debugger console

    Stepping, Stepping Into, Stepping Over: Causes the IDE to "enter" into a function that is called at that line. You will jump to the point that the function starts, and you can run one step at a time of the called function. This is called stepping, The middle controls in Spder give you controls to: run current line, sttepping into a function, and stepping out of the function (running until the return.) See figure 4S.
    spyder debug
    Figure 4S: The Step Into button in Spyder's debugger console


    Figure 4P shows how to step into a function in PyCharm.

    The Step Into button in PyCharm's debugger console

    Figure 4P: The Step Into button in PyCharm's debugger console

    Step return or Step out: Step out will tell the IDE interpreter to execute the rest of the lines in a function and pause after the function call is over. Figure 4 above also shows the step out button, one or two buttons to the right of the step in button in each IDE.

    The Resume Program button: This button exits debug mode and enables regular runs of your program once you are done debugging (see Figure 5).
    Spyder resume
    Figure 5S: The Resume Program button in Spyder's debugger console

    The Resume Program button in PyCharm's debugger console

    Figure 5P: The Resume Program button in PyCharm's debugger console


Your Team Assignment

Here are some buggy Python code samples which you and your partner will fix in this assignment:

All partners should also create a Microsoft Word file called yourusername(s)-t7.docx where yourusername(s) is appropriately replaced.

The instructions

This Paired Assignment has several steps:
  • Remember that the primary objective is to learn to use the debugger effectively, so please do not think that the objective is to just fix the errors--fixing the errors is just a vehicle to using the debugger.
  • Create a new folder called yourusernames-t7 into which you will return repaired programs:
  • Download the buggy python code samples which you and your partner will fix in this assignment:
  • In the header of each of these programs, put both partners names as having modified the code.  Also, include the role each partner played.  e.g. navigator, driver, etc.
  • For each of these programs, look carefully at the syntax and flow of control. Each of these programs has multiple problems which you should repair. These are marked with the IDE keyword FIXME.
  • The debugger is generally useful for looking at the values of variables without having to add print lines, and also to identify at what point a program crashes. Be sure to use each of the following debugger techniques:
    1. Setting a breakpoint in a function to look at the values of the variables which are local to the function.
    2. Running the code to the breakpoint in debugger mode.
    3. Stepping through code line by line while watching the variables.
  • Be sure to add comments to the code describing each of the things you fixed and why you needed to fix them.
  • In the report, put the assignment name and partners names and roles on top. Then answer the following questions:
    1. Which feature of the debugger did you find the most useful for this assignment? For what error did you find it useful?
    2. What kind of bugs do you think using a breakpoint would be useful for?
    3. What kind of bugs do you think stepping through code line-by-line would be useful for?
    4. How much do you foresee you and your partner using the debugger in future assignments? Explain.
  • When complete, one partner should put the repaired programs in the folder, zip the folder, and upload into Moodle.
  • Remember that you need to submit a single folder per team and that you must include both team members' names and the roles you had in each of the python program files.  It is also a good idea for the partner who is not submitting to upload the name of his or her partner.

Note that submitted files with incorrect filenames may not receive full credit because it makes grading more difficult for us and the TAs, so please check filenames carefully!


Copyright © 2016 | http://cs.berea.edu/courses/CSC226/ | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License