Debugging in Thonny

Sometimes, it can be difficult to understand exactly why the code that you wrote isn't working. Error messages can be cryptic, or perhaps you aren't getting any error message, but things aren't working correctly.

Thonny has a helpful feature called a "debugger," which lets us walk through our program step-by-step to actually see what it is doing. At the top of the Thonny window, next to the run button, you will see a series of buttons that look like this:

The buttons at the top of the Thonny window starting with the run button (a white triangle in a green circle). Next to that there's a debug button, whose icon looks like a bug, and then a series of grayed-out buttons with different yellow arrows on them. Finally, there's the STOP button, which is a red stop sign.

To activate the debugger, simply click on the button that looks like a bug (the creepy-crawly kind), instead of the normal run button. When you do that, the buttons will change so that they look like this:

The same buttons at the top of the Thonny window starting with the run button. Now the debug button is grayed out, but the debugging controls are active: a yellow arrow curving over a blue underscore for

These buttons allow you to control the flow of the program step-by-step, and Thonny will show you exactly what is happening by highlighting the part of the code being evaluated, and as you step through, showing each step of the simplification process for an expression.

Also note that if you're doing a screen share and only sharing the Thonny window, your partner will not be able to see the debugging information, and you'll have to share your whole desktop to get things working.

Try it out

Open the debugMe.py starter file and look at the code:

"""
Authors: Peter Mawhorter
Consulted:
Date: 2021-9-8
Purpose: Example code for use with the debugger.
"""

print("Probability calculator:")
print("The probability of success given multiple chances.")
baseProb = float(
    input("What is the probability of success for a single attempt? ")
)
attempts = int(input("How many attempts are allowed? "))

# The chance of all-failure is 1 minus the chance of success, to the
# power of the number of attempts
allFailedProb = (1 - baseProb) * attempts

# The chance of at least one success is one minus the chance of all
# failures
atLeastOne = 1 - allFailedProb
# Round and convert to a string to display the result

display = str(round(atLeastOne, 4))

print("The chance that at least one attempt will succeed is: " + display)

This code is intended to result in the following output (inputs in blue):

Probability calculator:
The probability of success given multiple chances.
What is the probability of success for a single attempt? 0.1
How many attempts are allowed? 5
The chance that at least one attempt will succeed is: 0.4095

However, if you run it, you will see that it prints an incorrect result:

Probability calculator:
The probability of success given multiple chances.
What is the probability of success for a single attempt? 0.1
How many attempts are allowed? 5
The chance that at least one attempt will succeed is: -3.5

We can get a better sense of why this error occurs by stepping through the code one step at a time using the debugger. Click that bug-shaped button to run the file in debug mode to get started.

Using the debugger

The simplest way to progress is to just repeatedly click the "step into" button (the middle button with the arrow pointing between the blue lines). Doing so will show you every single step of the program. Once you get used to that, you can try the "step over" and "step out" buttons. "Step over" will execute an entire line of code without going into the detailed steps within that line, and "step out" will keep going until the end of the current function call frame (we'll be talking about functions soon in lecture soon, and in lab next week). Finally, the "resume" button switches back to normal run mode and runs to the end of the program.

Use the "step into" button to move through the program one step at a time until it's finished. Pay attention to the way that Python processes your code, including what counts as a "step" and the order in which things occur. Pay close attention to what happens on lines 17 and 21, where the result probability is calculated.

Finding the error

Talk over the following questions with your partner or someone next to you:

  1. How many steps happen on line 17? How about line 21?
  2. Is the allFailedProb value computed correctly? What about the atLeastOne value? (remember that probability values should always be between 0 and 1).
  3. One line of code carries out a different calculation than what the comment above it claims. Which line is this, and which operation is wrong?

After discussing these questions, click below to reveal the answer.

Click here to show the answer On line 17, the comment claims that we should raise one minus the base probability to the power of the number of attempts. But the code uses '*', which is just multiplication. To perform exponentiation in Python, you can use the '**' operator. This could have been a very simple typo, but it's hard to spot unless you know what you're looking for.

Modify the code so that it works correctly. Verify by running it, and then run it in the debugger once more to see your fix in action. This time, you can use "step over" to skip over the lines that you don't care about, and use "step into" only where needed.

Bonus: Breakpoints

One last feature of the debugger that might prove useful: you can double-click on a line number in the left margin of your code to create a red dot on that line called a "breakpoint." When running the debugger, even when you use the resume button, it will always stop right before each breakpoint, so this can be a quick way to observe the state of your program at specific points without having to click through each step along the way to get there. To remove a breakpoint, simply double-click the line number again.

Table of Contents