Skip main navigation

Errors in Text-Based Programs

When using a text-based language, you can expect your programs to crash fairly frequently. A single misplaced quotation mark, 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 IDE has suddenly been filled with red text that might be barely comprehensible and therefore intimidating:

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 Programs

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 quotation mark, 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 IDE has suddenly been filled with red text that might be barely comprehensible and therefore intimidating:

Traceback (most recent call last):
 File "/home/mjs/test.py", line 2, in
<module>
 sleep ("10")
TypeError: an integer is required (got type str)

 

Debugging in Python

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 practise.

 

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 in Python. The programming environment, in this case Mu, caught the error and refused to try and run the program.

 

Animated gif of code missing a colon being run. The first line of the code states for i in range (10). The error message states: File "/User/matthogan/mu_code/b2t 26.py", line 1<br>for i in range (10) with an ^ under the bracket. The next line of the error message states SyntaxError : invalid syntax

 

The line where the problem has occurred is given in the error message and a ^ is used to indicate the position of the error. In this case, there’s a colon missing after the ) on line one.

Additional Bracket

Here’s another common error that is easily caught:

Animation of code with two brackets at the end of line one being run. Line one states print ('Welcome to the game!)) The error message states: File "/User/matthogan/mu_code/b2t 26.py", line 1 print ('Welcome to the game)) with a ^ under the second bracket. The final line of the error message states SyntaxError : invalid syntax

This error message occurs when the programmer enters two brackets where there should only be one.

Missing Bracket

This one can be a little trickier.

Animation of code missing a bracket at the end of line one being run. The first line states fruit_list = 'apple', 'banana', 'pear' 'strawberry'. The square bracket is missing at the end of the line. Line two states bag_list = 'banana', 'pear'. The error message states: File "/User/matthogan/mu_code/b2t 26.py", line 2 bag_list = 'banana', 'pear' with a ^ under the t of list). The final line of the error message states SyntaxError : invalid syntax

Mu seems to suggest that the error is on line two. However, it’s actually the failure to close the square brackets on line one that is the problem.

Incorrect Quotation Mark

In this example the programmer has used a double quotation mark matched with a single quotation mark. Quotation marks should be the same around a single string — they are not interchangeable.

Animation of code being run with a double quotation mark matched with a single quotation mark. This error is in line one which states: print ('Welcome to the game"). The error message states: File "/User/matthogan/mu_code/error.py", line 1 print ('Welcome to the game!") with a ^ under the closing bracket). The next line of the error message states SyntaxError : EOL while scanning string literal

Debugging discussion

Debugging becomes easier as you become more familiar with the Python language and understand the common errors you make.

  • How would you introduce debugging to learners new to text-based programming?
This article is from the free online

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

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now