3.1 Errors, Bugs and Debugging#
Errors are essentially flaws in the code that either prevent the code from compiling, running or producing correct results. Errors are also commonly referred to as bugs, and the process of fixing them as debugging.
In Python, there are 3 main types of errors to consider:
Syntax errors
Exceptions
Semantic errors
which we will discuss below.
General structure of an error message…
Syntax Errors#
Syntax errors (also known as parsing errors) are caused by incorrect use of syntax (how the code is written). These errors are found before the code is executed.
Note that sometimes the error message may indicate a line that is below the actual error (an example of this is provided below).
Examples#
Here are some examples of common syntax errors. This is by no means a comprehensive list.
Example
Using a Python keyword as a variable name:
yield = 5
File "C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\3601255470.py", line 1
yield = 5
^
SyntaxError: assignment to yield expression not possible
Example
Not closing quotation marks for string:
var = 'hello
File "C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\352281301.py", line 1
var = 'hello
^
SyntaxError: EOL while scanning string literal
Example
Missing colon at end of for loop:
for i in range(10)
File "C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\1522442676.py", line 1
for i in range(10)
^
SyntaxError: invalid syntax
Example
Inconsistent indentations:
if 2 < 5:
print('Hello')
print('World')
File "C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\2614729471.py", line 3
print('World')
^
IndentationError: unexpected indent
Example
Sometimes the error is in the line above:
print('Line1')
print('Line2'
print('Line3')
File "C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\3654184207.py", line 3
print('Line3')
^
SyntaxError: invalid syntax
Exceptions#
Exceptions (also known as runtime errors) are detected during code execution. There are many different types of these errors, so read the error message carefully, paying special attention to:
The line in your code where the error occurs.
The last line of the error message, which tells you the type of the error, and additional context.
If you want to intercept these errors in your code (to either make contingencies or to create custom error messages), then you can read-up on Handling Exceptions in the Python documentation. This is beyond the scope of this course.
Examples#
Below are some examples of exceptions. As before, this is not a comprehensive list of all possible errors.
Example
Trying to access the index of a collection (string) that is out of bounds:
my_string = 'string'
my_string[20]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_5216\4121466094.py in <module>
1 my_string = 'string'
----> 2 my_string[20]
IndexError: string index out of range
Example
Trying to use a variable that doesn’t exist:
my_variable = 6.5
my_var += 1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1208\3371564433.py in <module>
1 my_variable = 6.5
----> 2 my_var += 1
NameError: name 'my_var' is not defined
Double check your spelling and look back at where you first instance the variable if the cause of the error isn’t clear.
Example
Treating a non-callable object (function) like a callable:
3(2 + 3)
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
C:\Users\mayhe\AppData\Local\Temp\ipykernel_1208\1846702493.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
3(2 + 3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1208\1846702493.py in <module>
----> 1 3(2 + 3)
TypeError: 'int' object is not callable
This exception can come up fairly often when you are grouping operations. In the example above the intended operations were \(3 \times (2 + 3)\), however the multiplication (*
) was omitted.
Semantic Errors#
Semantic errors refer to when the code executes without exceptions, but produces “unexpected” results. There isn’t a flaw in how the code itself is written, but in the programmers onw logic.
Semantic errors can be difficult to find. A general strategy to solve these is to generate a hypothesis of where the problem may lie and test that. To avoid semantic errors:
Write your code in small increments and run it each time.
Probe your code using
print
function calls or use breakpoints and inspect variable values.
The strategies discussed in 3.2 Problem Solving Strategy are largely devised to catch semantic errors early in the problem solving process.