Explicitly teaching Python debugging strategies provides students with a toolkit of techniques. The same debugging strategy cannot be applied to every problem. Provide students with a multitude of strategies and explain when they are most effective. This will enable programmers to thoughtfully debug their programs using a toolkit full of techniques.
10 Python Debugging Strategies for Beginners
Do not wait until the end of a programming unit to teach Python debugging strategies. Instead, help students find and fix bugs. Below are 10 techniques that will build confidence and independence in your students. They are based on using IDLE Python to write code. Many of these strategies are included in the Python project TechnoTurtle for elementary and middle school students.
1. Look Before Highlighted Word to Find Syntax Errors
Syntax is the set of rules for how to order words and use symbols to write code. If a bracket, colon, or indentation is missing the IDLE Python program will show a Syntax Error box on the screen to identify the problem. It will also highlight a word to pinpoint the location.
Do not be fooled! The problem is not the highlighted word. Instead the mistake is BEFORE it. Look at the words just in front of the highlighting, or the line of code above to find the error.
Syntax errors are often typos. Missing commas, hashtags, or too many brackets are common. If that isn’t the issue, study the order of the words as they might be put together wrong.
Explicitly teaching Python debugging strategies. A Syntax Error is often missing symbols such as a colon, comma, or hashtag. Tell young programmers to look before the highlighted word.
2. Indent a Block of Instructions the Same Number of Spaces
Instructions that loop or run when a condition is met, are grouped together by indenting the lines of code to form a block. For example:
# correct way to make a loop
print(“You are right.”)
# wrong way to make a loop
print(“You are right.”)
When the code is not written correctly, IDLE Python will show a Syntax Error that states, “expected an indented block”. When this happens, check to see if the first line ends with a colon. If it does, then verify that each instruction in the block indents the same number of spaces.
3. Skim and Scan the Error Message to Find Clues
IDLE Python will display an error message in the Python Shell. It is in red text. To a novice, it can seem like gibberish. The best thing to do, is to scan the text for familiar words. Look for the line number. This will tell you where the error happened. To quickly go there within the program, right click the line number and select Go to file/line.
Before you view the line of code, study the bottom of the error message for more clues. It may say NameError at the bottom. If it does notice if the word is spelled correctly. If it is then check to see if a library was not imported or a variable was not defined properly.
Scan the message for clues. Look for the line number. Go to the last line to see if a function is spelled wrong. If it is right, you may have forgotten to import a library.
4. Look at the Color Coding
IDLE Python applies a color theme to text. This makes types of Python words different colors. This can be helpful when trying to find mistakes. Here are some helpful Python debugging strategies for beginners:
- Comments are red. If a comment is not red, it is likely missing a # at the beginning, such as #this is a comment.
- Strings are green. If a string is not green, it is likely missing quotes around it. Or, if a bracket is green, it likely that the end quote is missing.
Use color coding as a clue to find and fix errors.
5. Apply Trial and Error
A proven Python debugging strategy is Trial and Error. Trial and Error is a method of testing different ideas until one is found that works the best. The programmer will run their program and then study the output. They will notice the things that are wrong or unexpected. This analysis allows them to come up with a better idea to try next.
Trial and Error is systematic. The programmer only tries one idea at a time. This way they can pinpoint what is most effective.
Trial and Error is best applied when a programmer is fine-tuning the output. Their code has no syntax or naming errors. Instead, the focus is on discovering the ideal value or sequence of instructions to achieve the goal.
6. Code One Line at a Time
This Python debugging strategy can be used while writing a program or after it is written. When programming, type a line of code at a time and then run the program to test it. This allows you to quickly identify if there are typing errors or an issue with the sequence of instructions. This makes mistakes easier to spot.
Once a program is written, if you cannot find the mistake, divide the code into parts. Copy a chunk at a time into a new program. Run it to see it that section is error free. If it is, add another chunk of code to the program. Continue until you find where things are broken. Now you can focus your attention on that area.
7. Compare Using a Similar Model
If you cannot figure out what is wrong with the code and everything seems right, another Python debugging strategy is to find a similar program. Study the code to notice how it is constructed. This can provide a clue as to why the code is not working properly.
8. Copy and Paste from a Working Program
If you cannot debug the code and you are getting desperate, refer back to your previous work. Use existing code that is error-free as a jumping-off point. Copy a snippet of code that does a similar task from an existing program and paste it into the current file. Adjust the values or text to suit your programming objectives.
9. Enlarge the Font Size to Make the Code Easier to Read
When you have been programming for a long time the text can blur together. To make the lines of code sharper you can increase the font size or change the typeface to something that is easier to read. Now typos might be easier to spot. To do this:
- From the IDLE Editor Window, select the Options menu.
- Select Configure IDLE
- Click the Fonts/Tabs.
- Pick a font face such as Arial.
- Near the bottom pick a size such as 14.
- Click OK.
- Click the X to close the window.
Increase the font size and change the font to make bugs easier to spot.
10. Ask a Peer for Help
If you cannot find the error ask a friend for help. Often a fresh set of eyes can easily spot the typo in a line of code that has been looked over a hundred times. If the problem is related to the output, explain what you want the code to do. Look at the code together. Describe what is going right, and pinpoint where things start to go wrong. Run the program and study the outcome. Work together to discover a solution.
Python Debugging Strategies Support Learning
Make time to teach Python debugging strategies. Understanding how to code should not be restricted to those with a natural ability. Instead, provide your students with a toolkit of techniques they can use to find and fix errors. This will develop a positive attitude towards programming, build confidence, and promote independence.