''' DS2500 Spring 2025 Sample code from class - 1/28/25 Debugging - does our code work? Using the following foolproof method: 1. DON'T PANIC!!!!! 2. Scroll to the bottom and read the error message 3. Print out the variable value (print(x)) and/or data type (print(type(x)) to figure it out What errors did we see? 1. SyntaxError - for i in range(len(ages): What is the problem with a syntax error? Usually a typo! changed the line to: for i in range(len(ages)): 2. FileNotFoundError: [Errno 2] No such file or directory: 'boston_marathon_2022.csv' points to this line of code; with open(filename, "r") as infile: the problem is with filename, which is a paramter to the read_csv function so i look for where I call the function: data = read_csv(FILENAME, skip = 0) In this case, FILENAME was incorrect, should have been 2023 (another common issue here is that we don't have the file in the same directory as our code) 3. NameError: name 'csv' is not defined Often with a name error, it could be a typo? In this case, it's not a typo ("csv" is correct) but I forgot to import it (note: importing != installing. NameError means we forgot to import. ModuleNotFoundError means we forgot to install) 3. TypeError: unhashable type: 'list' pointing to this line of code: dct = {h : [] for h in lst[0]} Python is saying there's a problem with a dictionary key Dictionary keys cannot be lists (because lists can be modified) Start by reading the function documentation: given a 2d list, create and return a dictionary Let's check - do I give the function a 2d list? I call the function with dct = lst_to_dct([data]) HEre's the fix, get rid of [] around data: dct = lst_to_dct(data) 4. TypeError: 'int' object is not subscriptable Pythonn is saying that I'm trying to lst[i] to something that is not a list I'm treating an int as if it's a list 5. NameError: name 'all_ages' is not defined Common thing with NameError - typo? ''' import matplotlib.pyplot as plt from utils import read_csv, lst_to_dct FILENAME = "boston_marathon_2023.csv" BIB_HEADER = "BibNumber" AGE_HEADER = "AgeOnRaceDay" RANK_HEADER = "RankOverall" GENDER_HEADER = "Gender" TIME_HEADER = "OfficialTime" def filter_age(target_age, ages, times): ''' given a target age, a list of ages, and a list of times, filter and return a list of times for runners who are the given age OR OLDER ''' filtered_times = [] for i in range(len(ages)): if ages[i] >= target_age: filtered_times.append(times) return filtered_times def main(): # Gather data - read in the MBTA speed restrictions as a 2d list # and then convert to a dictionary where keys come from the header data = read_csv(FILENAME, skip = 0) # add a print here to see what I'm passing to lst_to_dct print(f"About to call lst_to_dct with {data}") dct = lst_to_dct(data) print(dct) # pull out a list of ages ages = dct[AGE_HEADER] print(f"Ages is a list before the loop {ages}") for i in range(len(ages)): #print(f"Ages in loop became an int: {ages}") ages[i] = int(ages[i]) print(f"Ages of runners in 2023: {ages}") # filter to keep everyone older than me master_times = filter_age(45, ages, dct[TIME_HEADER]) print(f"Times of everyone 45 or older: {master_times}") if __name__ == "__main__": main()