Return to Course Home Page


Debugging with the VS code debugger

debug.png

When your code doesn’t work as expected, you might: 1. Use print statements 1. Ask ChatGPT what’s wrong with your code

Print statements can be annoying to put all over the place and sometimes ChatGPT doesn’t know. So what if you could step into your code and run it line by line, interactively, to figure out what was wrong? This is what VS code’s debugger does for you.

Setup

Get the VS code python extension. 1. Go to the left hand bar in VS code and click “Extensions” 1. Search “Python” 1. Install the extension called “Python” by Microsoft

Note: VS code has a known bug where sometimes the Debug this cell option disappears. If this happens, you unfortunately need to restart VS code. This is not an issue if debugging .py files though, which is likely where you’ll end up using the debugger the most anyways.

Using the debugger

We will practice using the debugger with practice 2-1, problem 4.

First, let’s check out how the debugger works by placing a breakpoint on the first line of the cell where we define variables and stepping through it.

See how the variables appear under your Run and Debug tab, and try using the Debug Console to print or manipulate the varibles as you step throught the code.

Code
# Import numpy for mean calculations
import numpy as np

tempF_2015 = [61.628, 61.7, 61.808, 61.448, 61.52, 61.538, 61.394, 61.52, 61.61, 62.042, 61.988, 62.168]
tempF_2016 = [62.186, 62.546, 62.528, 62.06, 61.79, 61.52, 61.61, 61.916, 61.718, 61.682, 61.736, 61.628]
tempF_2017 = [61.916, 62.132, 62.168, 61.772, 61.718, 61.376, 61.556, 61.646, 61.466, 61.7, 61.664, 61.754]
tempF_2018 = [61.556, 61.61, 61.664, 61.682, 61.556, 61.466, 61.556, 61.448, 61.52, 61.916, 61.556, 61.718]
tempF_2019 = [61.754, 61.79, 62.186, 61.898, 61.61, 61.7, 61.772, 61.79, 61.754, 61.898, 61.862, 62.042]
tempF_2020 = [62.186, 62.312, 62.186, 62.114, 61.898, 61.736, 61.7, 61.646, 61.862, 61.664, 62.06, 61.538]
tempF_2021 = [61.538, 61.232, 61.664, 61.43, 61.484, 61.592, 61.736, 61.556, 61.736, 61.88, 61.772, 61.61]

# List of yearly lists (may or may not be useful)
tempF_list = [tempF_2015, tempF_2016, tempF_2017, tempF_2018, tempF_2019, tempF_2020, tempF_2021]

# List of years (probably useful)
years = [2015, 2016, 2017, 2018, 2019, 2020, 2021]

Being able to walk through code line by line is especially helpful when you can step into loops or functions that you would otherwise need print statements to see what is happening inside of.

Try placing a breakpoint on the first line and seeing how the variables change when you step through this for loop.

Code
for tempF_year,year in zip(tempF_list,years):
    print(f"{year} temperatures: {tempF_year}")
[61.628, 61.7, 61.808, 61.448, 61.52, 61.538, 61.394, 61.52, 61.61, 62.042, 61.988, 62.168]
2015 temperatures: [61.628, 61.7, 61.808, 61.448, 61.52, 61.538, 61.394, 61.52, 61.61, 62.042, 61.988, 62.168]
2016 temperatures: [62.186, 62.546, 62.528, 62.06, 61.79, 61.52, 61.61, 61.916, 61.718, 61.682, 61.736, 61.628]
2017 temperatures: [61.916, 62.132, 62.168, 61.772, 61.718, 61.376, 61.556, 61.646, 61.466, 61.7, 61.664, 61.754]
2018 temperatures: [61.556, 61.61, 61.664, 61.682, 61.556, 61.466, 61.556, 61.448, 61.52, 61.916, 61.556, 61.718]
2019 temperatures: [61.754, 61.79, 62.186, 61.898, 61.61, 61.7, 61.772, 61.79, 61.754, 61.898, 61.862, 62.042]
2020 temperatures: [62.186, 62.312, 62.186, 62.114, 61.898, 61.736, 61.7, 61.646, 61.862, 61.664, 62.06, 61.538]
2021 temperatures: [61.538, 61.232, 61.664, 61.43, 61.484, 61.592, 61.736, 61.556, 61.736, 61.88, 61.772, 61.61]

Now let’s try debugging some code. Suppose your friend has written some code to solve problem 4a but is running into an error. Let’s try using the debugger to fix it:

Practice 2-1 problem 4a: Calculate the monthly global temperature anomalies (deviation from the mean) in °C for 2015-2021. The mean global land-ocean surface temperature calculated over the 20th century was 15.6°C.

Code
# 4a. Monthly temperature anomalies

# Innitialize a list of yearly lists of anomalies in °C
anomC_list = [] 

# Iterate through yearly lists
for tempsF in tempF_list:
    # Empty list for a single year
    anomC = []
    # Iterate through a single year
    for tempF in tempsF:
        # Convert temp from F to C
        tempC = tempF - 32.0 * (5/9)
        # Calculate temp anomaly in °C
        anomC = tempC - 15.6
        # Append to anomC
        anomC.append(round(anomC,2))
    # Add list of anomalies in °C to anomC_list (list of lists)
    anomC_list.append(anomC)

for anom_list,year in zip(anomC_list,years):
    print(f"{year} anomalies: {anom_list}")
28.25022222222222
AttributeError: 'float' object has no attribute 'append'

Now you try it on your own. Try using the debugger to debug the following answer to question 4b:

Create a new list with the mean monthly global surface temperature anomalies in °C for 2015-2023 (i.e. calculate the mean temperature anomaly for each month and put these values in a list).

Code
# 4b. Mean monthly temperature anomaly, 2015-2021

# Empty list for monthly mean temperature anomalies
monthly_means = []
# Set up generic counter loop with 12 iterations
for i in range(12 + 1):
    # Generate list of temperature anomalies for each month by extracting the ith value from each sublist.
    monthly = []
    # generate a list of anomalies for month i with anomalies from all years in it
    for anom in anomC_list:
        # get the ith anomaly (ith month) from each year list
        monthly.append(anoms[i + 1])
    # Calculate mean for month i
    monthly_mean = np.sum(monthly)
    # Add mean for month i to list of means
    monthly_means.append(round(monthly_means,4))

# Print list of mean monthly temperature anomalies, 2015-2018.
print(f"monthly_means: {monthly_means}")