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

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.

Here’s another common error that is easily caught:

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.

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.

### 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?