Custom Search

Error Messages

Computers are logical and precise. The program you write has to be correct in its syntax for the computer to run it.

Computers do not 'guess what you meant' if you make a spelling mistake and 'correct' your spelling like a word processor program would do.

If, for example, you type 'pint' instead of 'print' the computer will not know what you want to do as 'pint' is not part of its programming language vocabulary. It will tell you there is an error - as it does not understand the command - and it will point out the line of code on which it encountered the problem. It will report the error and stop trying to run your program.

The same is true if you miss punctuation - for example if you forget the comma between items or miss the quotes from around a text string - the computer will give up on understanding and running your code and will display an error message for you.

Most error messages are rather cryptic. You will get used to them, but at first you may not know what the computer means when it points out an error to you.

The OU course suggests that you do errors on purpose so that you can find out the types of message you will get.

Let's write a simple sum program - 12 x 4 - 2

Now try putting double-quotes around the 12 on the first line and run the program.

So our error is in line 1.

An operand is a mathematical term for the numbers in the sum... but in putting quotes around the number 12 it stopped being an integer and became a string.

'int' indicates an integer type

'str' indicates a string type

Your program was trying to multiply a string by and integer - that did not compute!

So we got an error message saying that there is a type error.

This basically meant the code is mixing data of different types in an equation.

 

Sometimes, code has more subtle errors, that can lead to unexpected results - and no error message.

This is rather like miscommunication, when people understand what you say to mean something different from what you intended to convey to them.

Here is an example. Let's do a simple sum and call the answer simply - result.

So, result is a variable with a numeric value that we set by entering our simple sum into the code.

result = 1 + 3

Now we get the computer to print the sum and its answer:

print ("The sum of 3 and 7 is ", result)

If you get carried away and add quotes to result the computer then thinks of it as a text stringl.... but you get no error message because the computer thought it had done what you wanted it to....

print ("The sum of 3 and 7 is ", "result")