Debugging Under Idle

 

Enable the debugger from the shell: click on debug then debugger. You see a window like this:

In the shell you see:

[DEBUG ON]

>>> 

Open a program source code file from the shell: file-->open-->retirement.py 

This is a sample program from chapter 3. It is also available to you from this link. You can copy it to your Python directory. Open the source code in an Idle window.

Start a program once debugging is  on from its source code window: Run-->Run module (or press F5)

You can now run the program under the debugger.

 

You usually want to set a breakpoint in your program before starting it. When the debugger encounters a breakpoint while running the program, it stops execution before the statement is executed. You set a breakpoint on a line by right clicking when the cursor is on the line, then click “Set Breakpoint”. Set a breakpoint on line26  in retirement.py:

main()

The line now is highlighted with a yellow background.

You start the program by clicking the go button in the debug control window. The program stops on the first breakpoint it encounters, or when it is waiting for keyboard input.  After clicking on go, the program stops at your breakpoint on line 26:

 

Once a program is stopped at a breakpoint, you can start in different ways by pressing the Debug Control buttons:

Go starts the program. It runs until it hits another breakpoint, waits for keyboard input, or the program ends.

Step executes the next statement. If it is the breakpointed statement, that statement is executed. If the statement is a function call (as this one is), the debugger enters the called function

Over executes the next statement as step does, but does stays in the current code. It does not enter any functions.

Out exits the function that it is in, returning control to the caller.

Quit ends the program.

As an example, set a breakpoint on line 8:

show_pay_contrib(gross_pay)

It is now highlighted in yellow. Press Go. The program runs to the line:

gross_pay = input('Enter the gross pay: ')

and waits for your input. Enter 8000.00 for gross pay, and 2000.00 for bonuses. After entering the bonus, the program runs to the breakpoint on line 8 and you see in the debug control window:

Notice that the line number where the program is stopped (8) is in the top of the screen under the control buttons. The contents of the variables bonus and gross_pay are shown at the bottom under “Locals”.

Press Step. The line number at the top of the screen is now 15 (in show_pay_contrib). Press Step again, you have moved to line 16, and under Locals, you now see the two variables contrib, and gross. Press Step again and line 16 gets executed, and you see in the Python shell:

Contribution for gross pay: $400.00

Press Step again and you are now on line 22 (in show_bonus_contrib). Press Step again and line 22 is executed and you see the bonus and contrib variables under Locals. If you press Go, the program runs to completion.