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.
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 inzip(tempF_list,years):print(f"{year} temperatures: {tempF_year}")
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 °CanomC_list = [] # Iterate through yearly listsfor tempsF in tempF_list:# Empty list for a single year anomC = []# Iterate through a single yearfor 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 inzip(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 anomaliesmonthly_means = []# Set up generic counter loop with 12 iterationsfor i inrange(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 itfor 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}")