Problem in guided project: prison break

min_year = min(data, key=lambda x: x[0])[0]
max_year = max(data, key=lambda x: x[0])[0]
years = []
for y in range(min_year, max_year + 1):
    years.append(y)
    
attempts_per_year = []
for year in years:
    attempts_per_year.append([year, 0])

print(attempts_per_year)
TypeError                                 Traceback (most recent call last)
<ipython-input-41-7fa204666547> in <module>
----> 1 min_year = min(data, key=lambda x: x[0])[0]
      2 max_year = max(data, key=lambda x: x[0])[0]
      3 years = []
      4 for y in range(min_year, max_year + 1):
      5     years.append(y)

TypeError: '<' not supported between instances of 'int' and 'str'

Why do i get this error? Help me :frowning:

This error might be as a result of having instances of integers and strings. Do a double check on your data to see where there may be strings.

i suggest you put each line of code in a separate cell and run each individually before proceeding to the next line of code in another cell.
in this particular case, i suggest you first run the code for min and max year and print it and check if you’re getting the correct value.

The error is due to “Chapter 8: extracting the year”
The second instruction in chapter says " 2. Print the first few rows of data to confirm that you executed the task correctly."

Here you may have added something like below to get few rows from data list:
for row in data[:3]:
year = fetch_year(row[0])
row[0] = year
print (row[:-2])

Remove this limitation as below, this is how I resolved this exact same error.
Fix:
for row in data:
year = fetch_year(row[0])
row[0] = year
print (row[:-2])

Now run the code from line 1.

2 Likes