Skip main navigation

Top Debugging Tips

Learn more about finding and fixing errors in your Python code.

Scratch programs don’t really crash. They might not do anything, or they might not do what is expected, but you won’t ever see error messages being spoken to you by the orange cat. Text-based languages are a different story, and it is important for learners to understand how to debug a text-based program when things go wrong.

When using a text-based language, you can expect your programs to crash fairly frequently. A single misplaced quote, a missing colon, or a lower-case letter where an upper-case letter should be, and not only will your program refuse to run, but you’ll find that your shell has suddenly been filled with red text that might be barely comprehensible and therefore intimidating.

2-5-1.png

The moment a student new to Python sees such a thing, their most likely response will be to wave their hand in the air and ask you why it’s not working. However, with a little coaching and some careful explanation, you can help your learners develop the debugging skills they need to fix the issues in their programs themselves.

Types of Error

There are three types of error that can occur in a program: syntax errors, runtime errors, and semantic errors. The first is fairly simple to detect in any common programming interface. The second may be easy to spot, but this depends on the actual error. The last might be difficult to spot, but doing so gets easier with practice.

Syntax Errors

These occur when the programmer doesn’t follow the rules of the programming language. Sometimes the programming environment will simply refuse to even try and run the program.

Missing Colon

Here is an example of a fairly simple syntax error. The programming environment, in this case, IDLE, caught the error and refused to try and run the program.

2-5-2.png

The area where the problem has occurred is highlighted in pink — in this case, there’s a colon missing.

Missing Quotation Mark

Here’s another common error that is easily caught:

2-5-3.png

This error message occurs when the programmer has forgotten or incorrectly closed, a quote around a string.

Missing Bracket

This one can be a little trickier.

2-5-4.png

The area highlighted seems to suggest that the error is on line 2. However, it’s actually the failure to close the square brackets on line 1 that is the problem.

Runtime Errors

All these basic syntax errors are fairly easy to spot. Things can get a little trickier when Python provides a traceback error. These are a form of runtime error. They are not caught until the program runs, at which point a line of code that the interpreter can’t run will lead to a traceback error getting printed in the shell.

 

If you run this code:

 

while true:
 print('hello') 

 

The following error is printed:

 

2-5-5.png

 

The error here is that true has been used with a lower-case t — it should be True. So why wasn’t this caught as a syntax error? The reason is that the interpreter didn’t recognise true as a keyword and assumed that it was a variable. Then when it tried to find the true value, it discovered that it had no value and so kicked back a traceback error.

 

The code can be fixed easily. Simply replace true with True, and it will work.

 

while True:
 print('hello')

 

But just to illustrate the nature of the error, you could also do the following and the code will work.

 

true = True
while true:
 print('hello')

 

Figuring out traceback errors comes with experience. The first thing to realise is that you read the error message from the bottom upwards.

 

2-5-6.png

 

Here the message is saying ImportError: cannot import name 'slep', telling us that the interpreter doesn’t know what slep means.

 

Above that is the message from time import slep, showing the exact line of code where the error has occurred. So in this case, slep needs to become sleep.

 

Semantic Errors

 

Have a go at running the following lines of code, see if you can see where the errors might be, and fix the script until it works.

 

while my_num != 6
 my_num = input('Guess what number I'm thinking of ')
 if my_num > 6:
 print('Too high')
 elseif my_num < 6:
 print('Too low')
 else:
 print('You guessed it')

 

Semantic errors are the hardest to spot: your script will run without any problems, but it won’t produce the result you expected. There are no special tricks to spotting semantic errors. You can use a debugger, or lots of print() statements to query the values of variables and the outputs of functions, but in the end, this is going to take practice. Below is a script with a semantic error — can you spot it?

 

num = int(input("Give me a number "))
if num > 10:
 print('Your number is greater than 10')
else:
 print('Your number is less than 10')

It is important to remember that all programmers deal with errors every day. In fact, often they know something is definitely wrong with their code if it runs the first time without any errors. That’s too good to be true, like writing a whole novel without your word processor spotting a single spelling mistake!

  • Did you find all the errors and manage to fix them?
  • How have you coped with error messages and bugs up until now?
  • How do you think your learners will cope?

 

This article is from the free online

Scratch to Python: Moving from Block- to Text-based Programming

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education