Troubleshooting and “Debugging” Python Programs

Troubleshooting and “Debugging” Python Programs#

Troubleshooting, also called “debugging” in computer science, is the process of finding and fixing errors in your code. It is an important skill for any programmer. In this worksheet, we will practice debugging by finding and fixing errors in some code. You should try to do this without writing code first. Just read the code and try to predict what it will do and how to fix it. After you have made predictions about the code, you can run it in Python to check your answers.

Reading Python Error Messages#

When you run a Python program, you may see an error message. The error message will tell you what went wrong and where it went wrong. In Python, when you get an error, you should start reading at the bottom of the traceback. The bottom of the traceback will tell you what went wrong. The message will also show the code where the error occurred, and show an arrow pointing to the exact location of the error.

_images/annotated_traceback.png

In the example above, the error message tells us that there is a NameError. A NameError occurs when you try to use a variable that has not been defined. In this case, the variable energy_kcal has not been defined. Yesterday in our code, we fixed this by adding energy_kcal = [] before the for loop.

Some common types of errors in Python are:

  • NameError: You tried to use a variable that has not been defined. Sometimes this might tell you that you have a typo in your variable name.

  • SyntaxError: You have a typo in your code. For example, you forgot a colon at the end of a line.

  • IndentationError: Your for loop or if statement is not indented correctly.

  • TypeError: You tried to use a variable of the wrong type. For example, you tried to use a string as a number.

  • ValueError: You tried to use a variable with the correct type, but the wrong value. For example, you tried to use a number that is too large or too small.

  • FileNotFoundError: You tried to open a file that does not exist. Sometimes you might have just made a typo in the file name.

Debugging Practice#

Practice reading the error messages below. Below each error message, write down what you think the error is and how you would fix it.

carbon = 12.011
hydrogen = 1

methane_weight = cabon * 1 + hydrogen * 4
print(methane_weight)

Use this box to identify the error that occurred above. How would you fix it?










x = 5
y = "2"
z = x + y
print(z)

Use this box to identify the error that occurred above. How would you fix it?










numbers = [1, 2, 3, 4, 5]
times_10_list = []

for num in numbers:
    times_10 = num * 10
print(times_10) 
    times_10_list = times_10_list.append(times_10)

print(times_10_list)

Use this box to identify the error that occurred above. How would you fix it?










Sometimes, the error message will contains code from multiple functions, with the error occurring at the place where the ----> arrow is pointing. This is called a “Traceback” because you can trace the sequence of function calls that led to the error. Sometimes when you see a traceback, the code that is highlighted will be from another function that you did not write.

Typically when you encounter a traceback, you will want to “trace back” the code until you find the part you wrote. Try to use this strategy in the example below.

ethanol_file = "data/outfile/ethanol.out"

with open(ethanol_file, "r") as outfile:
    data = outfile.readlines()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[2], line 3
      1 ethanol_file = "data/outfile/ethanol.out"
----> 3 with open(ethanol_file, "r") as outfile:
      4     data = outfile.readlines()

File ~/miniconda3/envs/molssi_best_practices/lib/python3.11/site-packages/IPython/core/interactiveshell.py:310, in _modified_open(file, *args, **kwargs)
    303 if file in {0, 1, 2}:
    304     raise ValueError(
    305         f"IPython won't let you open fd={file} by default "
    306         "as it is likely to crash IPython. If you know what you are doing, "
    307         "you can use builtins' open."
    308     )
--> 310 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: 'data/outfile/ethanol.out'

Use this box to identify the error that occurred above. How would you fix it?