4.1 Debugging#

It is very easy (and common) to make mistakes when programming. We call these errors bugs. Finding these bugs in your program and resolving them is what we call debugging.

Types of errors#

According to Think PythonAppendix A, there are three different types of errors:

1. Syntax errors#

”In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in that language.”

Therefore, a syntax error is an error that does not obey the rules of the programming language. For example, parenthesis always comes in pairs… so (1+2) is OK, but 1+2) is not. Below another example of a syntax error. As you will see — this error is caught by the interpreter before running the code (hence, the print statements do not result in anything being printed).

# I want to raise 2 to the 3rd power.
# However, I apply the wrong syntax, causing a syntax error:
print('Message before')
2***3
print('Message after')

2. Runtime errors#

”The second type of error is a runtime error. This type of error does not appear until after the program has started running. These errors are also called exceptions, as they usually indicate that something exceptional (and bad) has happened.”

Below an example of a runtime error:

# A small script to express fractions as decimals
numerators = [1, 7, 5, 12, -1]
denominators = [6, 8, -1, 0, 5]
fractions = []

for i in range(len(numerators)):
    fractions.append(numerators[i] / denominators[i])
    print(f'New fraction was added from {numerators[i]} and {denominators[i]}!\n It is equal to {fractions[i]:.3f}')
    # Error will appear, since you cannot divide by 0

3. Semantic errors#

According to the Oxford Dictionary, ‘semantic’ is an adjective relating to meaning. Therefore, a ‘semantic error’ is an error in the meaning of your code. Your code will still run without giving any error back, but it will not result in what you expected (or desired). For that reason, semantic errors are the hardest to identify. Below an example:

# I want to raise 2 to the 3rd power.
# However, I apply the wrong syntax that does not represent raising to a power.
# No error message is created, because this syntax is used 
# for another function in Python
# However, this results in an output I did not expect nor desire

power_of_2 = 2^3
print(f'2 to the 3rd power is {power_of_2}')

Debugging strategies#

There are a few ways to debug a program. A simple one is to debug by tracking your values using print statements. By printing the values of the variables in between, we can find where the program does something unwanted. For example, the code block below:

A = [0, 1, 2, 3]

def sumA(my_list):
    "returns the sum of all the values in a given list"
    my_sum = 0
    i = 0
    while i < len(A):
        my_sum = A[i]
        i += 1
    return my_sum

print(f'The sum of the elements of the list A is {sumA(A)}.')

We see that our sumA() function outputs \(3\), which isn’t the sum of the contents of the list \(A\). By adding a print(my_sum) inside the loop we can get a clearer understanding of what goes wrong.

def sumA(my_list):
    "returns the sum of all the values in a given list"
    my_sum = 0
    i = 0
    while i < len(A):
        my_sum = A[i]
        print(f'var my_sum[{i}] = {my_sum}')
        i += 1
    return my_sum

print(f'The sum of the elements of the list A is {sumA(A)}.')

It looks like the function is just stating the values of the list \(A\), but not adding them… so we must have forgotten to add something. Below the fixed version of that function.

def sumA_fixed(my_list):
    "returns the sum of all the values in a given list"
    my_sum = 0
    i = 0
    while i < len(A):
        my_sum += A[i]
        print(f'var my_sum[{i}] = {my_sum}')
        i += 1
    return my_sum

print(f'The sum of the elements of the list A is {sumA_fixed(A)}.')

Additional study material:

After this Chapter you should be able to:#

  • know different types of errors

  • have a plan when debugging your code