What is wrong with my Python code?
How Can We Help?
Teacher,
Cat enthusiast
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
There can be several things that we can get wrong when coding in Python which can be very frustrating!
Here is a step by step process to figuring out what is wrong:
What type of error is it?
First identify the type of error. It can be one of the following:
- Syntax error – this is where a symbol is missing, you have too many of them or something is misspelt. It can also be incorrect indentation
- Logical error – your program works but something is not working correctly. This could be something that isn’t in the right order (sequence) or the logic you have implemented is wrong for example not splitting a number off at the right point.
Syntax errors
Syntax errors are normally the easiest to fix because you should get a specific error message from Python that tells you exactly the error you have made and the line number the error appears on.
If you look at the line of code and you can’t see the error – check on the previous line. Sometimes errors on the previous lines can cause errors to appear on the next line down.
The most common types of syntax errors are:
Syntax error – check the line of code that this points to and ensure you have all of the brackets, speech marks and equal signs that you need.
Indentation error – you have not indented code as it should be. Remember that anything after a colon should be indented to the left.
Logical errors
Logical errors are often the hardest to find because sometimes the program can work but it does not behave as you would expect. This is where you may need to trace through your program and look at the values that certain variables are holding during the running of your program. Sometimes it is simply a case of changing the order (sequence) of code around or editing your logic slightly to produce the correct result.