Guided Project Prision Break error

Screen Link:

My Code:

years = []
for y in range(min_year, max_year + 1):
    years.append(y)
print(years)

What I expected to happen:
[1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]

What actually happened:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-36921ba71f63> in <module>
      1 years = []
----> 2 for y in range(min_year, max_year + 1):
      3     years.append(y)

TypeError: can only concatenate str (not "int") to str

I have no idea why this is happening! This is the code that DQ gives you, so I don’t know why it doesn’t work.

Hi @col1022 ,

In the previous step were you able to print the min_year and max_year? If so were they the numbers you were supposed to get?

If everything until this point was right, maybe try restart and rerunning your jupyter notebook.

If the issue still persist, please do share your notebook file as well, so that we can have a better look.

Can you also provide the code for min_year and max_year because according to the error, it looks like the problem is that these variables are of the type str and not int.

What do you get when you type:

print(type(min_year))
print(type(max_year))
print(type(min_year))
print(type(max_year))

<class 'str'>
<class 'str'>

Here is the notebook file too, so that you can look at the whole thing.
Basics.ipynb (7.9 KB)

Click here to view the jupyter notebook file in a new tab

After playing around with your notebook and checking with the guided project instructions (I haven’t done this one yet), I see that the problem begins on screen #8: “Extracting the Year.” I’m not sure why but the output doesn’t look right after you use fetch_year(); this function is supposed to extract the year from a str and return it as an int. That’s why you’re getting an error on screen #9 because the year is still the full date string (min_year and max_year are supposed to be int.)

Try going back to screen #8 and playing around with the fetch_year() function until you get the following output:

for i in data[:3]:
    print(i)

OUTPUT:
[1971, 'Santa Martha Acatitla', 'Mexico', 'Yes', 'Joel David Kaplan Carlos Antonio Contreras Castro']
[1973, 'Mountjoy Jail', 'Ireland', 'Yes', "JB O'Hagan Seamus TwomeyKevin Mallon"]
[1978, 'United States Penitentiary, Marion', 'United States', 'No', 'Garrett Brock TrapnellMartin Joseph McNallyJames Kenneth Johnson']

After thinking about it a bit more and confirming with your code, I can see where things have gone off the rails and why! Unfortunately it’s too good of a learning opportunity for me to just tell you outright :nerd_face:

After reading my reply above, my last hint is this: there is no typo or missing code in what you’ve got…it’s an “order of operations” problem.

Let me know how you make out and if you’re still stuck we can try something else together.

I went back to screen nine and realized that there were supposed to be 2 for loops, not 1. After making the for loops look like this:

for row in data:
    data [index]=row[0:-1]
    index+=1

for row in data:
      row[0] = fetch_year(row[0])

and not this:

for row in data:
    data [index]=row[0:-1]
    row[0] = fetch_year(row[0])
    index+=1

my code started working!

1 Like

Great, awesome job! Glad to hear you figured it out on your own; I find that’s the best way to learn!