This post covers two of the common error types in Python that you may encounter during your studies. It also covers common errors made when writing Pseudocode, because its super important to avoid these common errors in Pseudocode too as quite a lot of exam questions about programming can ask you to write Pseudocode and you don’t want these errors to hold you back from the marker seeing the depth of your understanding.
Error Types
Syntax errors are when part of the program has been written wrong to the point that it won’t function. It will cause an error, crash, and not proceed in running. Here is an example in Python:
That error message can be translated to mean that because there was a quotation mark missing, the program couldn’t find where the line ended, because it didn’t find the end quotation mark.
Logic errors are when the program runs correctly, but there may be numbers wrong here and there, or the wrong use of mathematical symbols.
Here is an example in Python, where a divisionFunction has been created, but instead of dividing the number we have added it:
As you can see, the program keeps running but the answer is wrong. This is how logic errors work.
Ways to avoid a syntax error
Sticking to the rules regarding variable names. If variable names don’t stick to these rules, your program will crash.
Cannot begin with numbers
Cannot contain spaces
No special characters (@, #, etc.)
Can only be made up of letters, numbers and underscores: _
They are CASE SENSITIVE
Making sure you have considered what data type you are using for Arrays, because they can only store date of one data type, and in Python programming. Here is a reminder of some common data types:
Strings - a string of text: a word or a sentence, surrounded by quotation marks
Integers - a whole number
Boolean - true or false
Float - a decimal number
Using the correct type of brackets for your task, and making sure you always have both opening and closing brackets
Functions ALWAYS have brackets around their parameters and arguments.
Arrays and lists ALWAYS use brackets around their contents
NEVER forget closing brackets, this is the most common mistake in programming.
For arrays we use square brackets: []
For function parameters and arguments we use curved brackets: ()
Making sure you have used indentation correctly (the space at the start of the line(s) of code inside if statements, loops or functions. Incorrect indentation can cause the program to cease working or to produce really unexpected outcomes. Indentation is shown by using:
The Tab key
Four spaces
Making sure all of your strings are surrounded by quotation marks. Otherwise the computer won’t know where the line stops, like in the example seen earlier.
Paying attention to your capitalisation. Everything in most programming languages, including Python, is case sensitive. It is also a good idea to capitalise key words in pseudocode to make them clearer.
All function names must begin with a lowercase character.
When we write pseudocode, all of our keywords must be CAPITALISED. We don’t benefit from the colour coding that a programming environment has and we have to show what our keywords are.
Python is a case sensitive language - if variable names are not recognised later in the program be sure to check their capitalisation - you may have capital letters where they don’t belong.
Do not use capital letters when trying to utilise Python’s built-in functions. It will not work.