Tag Archives: programming for kids

Teach Debugging to Beginners to Build Confidence

It is important to teach debugging to beginners right away. Debugging is when a programmer finds “bugs” or errors in their code and fixes them to improve the program. This is an essential programming skill. So much so, that it should be taught at the beginning of a coding unit.

Where Were the Debugging Strategies in the Instructional Materials and Python Courses?

When I was asked to develop Python materials for TechnoKids Inc. I jumped at the chance. I began to read books about Python. They had titles emphasizing that the instructions inside were for “absolute beginners”. I noticed that none had activities or advice on debugging. Instead, if the code I was copying from the page didn’t work I was left guessing why. It was frustrating!

My reaction was one that I knew students would feel. I worried that young programmers might begin to think “I am not good at programming” when they encountered an error they could not fix. This belief might turn them away from learning more about coding. I knew it was necessary to reframe how students respond to mistakes in their programs. They needed to feel empowered, not defeated.

The books were helpful in understanding the types of instructional materials available to teachers on teaching Python – but I needed to learn more!

I wanted to understand the difference between what young children and young adults were learning about Python. I signed up for a first year course at the university where I did my undergraduate degree. This course did have a section on debugging, but it was at the end of the course. I asked the professor why it was introduced so late and was told it was because beginners could not understand the error messages until they had learned how to code. As a result, I spent a good portion of the course inefficiently applying debugging strategies. I knew there had to be a better way!

When it came time to create the TechnoKids Python STEM projects I had reached a decision…debugging needed to be taught right away. I wanted students to be able to understand the meaning of the errors they would see. More importantly, I knew they needed simple strategies to fix their code.

teach debugging to beginners

Teach debugging to beginners.

What Happens When the Program Keeps Shouting ‘You Are Wrong’…But You Don’t Know How to Make it Right?

When Students Lack Debugging Strategies, Red X’s Tell the Learner They Can’t Code

During my investigation of Python programming with kids I noticed that there were common errors made. These mistakes would trigger several responses by the IDLE Python program. The Python Editor Window, which is where programmers build the code, will display a box with a red x on Windows devices. Those are for syntax or indent errors. In this case, there might be a forgotten bracket or # in front of a comment: simple to fix, but not without knowledge of debugging.

The angry red x’s that show up on the screen mock a novice. Unintentionally they seem to say “you can’t code”. To prevent students from becoming discouraged, teachers should provide strategies for decoding error messages. This will build confidence and promote independence.

syntax and indent error boxes in Python

When a student lacks debugging strategies the Python error messages can be a source of frustration.

When Students Lack Debugging Strategies, They Can’t Find the Problem

Another technique the IDLE Python program uses to assist programmers is to identify a spot within the code where there is an issue. This is accomplished with a reddish-pink box around text. The skilled programmer knows to look BEFORE the highlight.

However, those without debugging strategies can stare at the code forever and never know what is wrong. This is because children and young adults are literal. They look at the reddish-pink text and say “Oh, the problem is right HERE”. Beginners lack sufficient knowledge to locate the error. This shortcoming can easily be overcome by explicitly teaching debugging strategies.

Python error

The word highlighted is not the error. It is the missing bracket BEFORE shape.

When Students Lack Debugging Strategies, Red Text Tells a Beginner They Do Not Understand How to Program

The error response by the IDLE Python program is to show a message in the Python Shell. The text is red. It often tells the programmer there is a name or type error. These errors can occur if a library is not imported, a command is spelled wrong, a variable is not used properly in the code, or a sentence does not use the correct punctuation for joining text with variables. The Python message identifies the line number and reason for the problem. However, with no debugging strategies it can seem like gibberish.

The red text in the Python Shell is the equivalent of using a red pen to grade student work. Many educators have given up this practice as it promotes self-esteem issues, which can turn students away from wanting to learn. Studies have show that for many learners red pen is like ALL CAPS. It yells “You are wrong!” When red text is the method for highlighting errors, it can make a student feel angry or sad. This is avoidable when educators teach debugging to beginners at the start of a coding unit.

Python error messages can seem like gibberish to beginners.

Teach Debugging to Beginners at the Start of a Coding Unit

To avoid making students feel bad about themselves, why not just give students perfect code to copy? Then when they run the program, no errors will happen. Or if they do, they can quickly refer to their sample to find the typo. Problem solved!

No. Not really.

The limitation to this instructional approach is that it restricts learning. One of the goals of teaching a STEM project should be to support students so they produce their own original creations. Aside from developing programming skills, the learning objectives in a programming unit should include: fostering an interest in programming, developing computational thinking skills, and applying logic and reasoning to solve a problem. The aim should not be improving typing skills.

Spark an ongoing interest in programming. Teach debugging strategies at the beginning of a coding unit. This will build confidence and promote independence.

Python Lessons that Explicitly Teach Debugging to Beginners

Teachers need to deliberately teach debugging strategies at the start of a programming unit. This empowers students. Using this instructional approach, the TechnoTurtle STEM project includes activities that teach students about common errors and how to fix the code.

TechnoTurtle has 30 coding assignments. Elementary and middle students solve mazes, create artwork, and build games. Several of the lessons emphasize debugging strategies. They are interwoven throughout the project to introduce or reinforce computer science concepts:

  • Edit a Python Program: Modify values to understand the purpose of code.
  • Bug Zapper: Add mistakes to a program to learn how to debug Python.
  • Clean Up the Code: Fix coding errors by selecting from the list of choices.
  • Trial and Error: Test different ideas to move a Turtle through a maze.
  • Draw a Robot: Write one line of code at a time and make corrections as you go.
  • Edit an Invitation: Change code to discover how to join text with variables.

Python Debugging Strategies for Beginners

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.

Python debugging strategies

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
if answer==”tiger”:
    print(“You are right.”)
    print(“You win!”)

# wrong way to make a loop
if answer==”tiger”:
print(“You are right.”)
print(“You win!”)

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.

Python error messages

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.
Python color coding

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:

  1. From the IDLE Editor Window, select the Options menu.
  2. Select Configure IDLE
  3. Click the Fonts/Tabs.
  4. Pick a font face such as Arial.
  5. Near the bottom pick a size such as 14.
  6. Click OK.
  7. Click the X to close the window.
IDLE Python options

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.