Guided Project: Prison Break 9/14, Kunal's Question

Screen Link:

My Code:

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

What I expected to happen:

I have no idea

What actually happened:

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

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

Instructions

  1. Copy and paste the given codes into a new code cell.
  2. Create a list, attempts_per_year, whose elements all look like [<year>, 0].
  • The first element of each inner list should be one of the years of the list created in the second instruction.
  • There should be no repeated years
  1. Print the list above to track your progress.

can someone explain how, why, and what to use to solve this question? I’m completely lost.

Hello, @kunal.shukla285. Looks like you are trying add a string and an integer type. Can you post a screen capture of the cells above this code?

Please comment out these two lines:
#for y in range(min_year, max_year + 1):
#years.append(y)

Could you please print max_year and post the output.
print(max_year)

September 25, 2020 - this is my output. Can you explain the syntax? I don’t understand what lambda and the notation means with x. I haven’t seen this before in the curriculum.

1 Like

I tried posting a screen capture, but the uploaded screenshot just kept loading forever, so I couldn’t give you a screenshot. It never successfully uploaded.

Hi @kunal.shukla285

  1. In the code for y in range(min_year, max_year + 1): max_year is a string type value i.e made of alphanumeric characters. Also evident from the output for print(max_year) which is “September 25, 2020”.
    Since it’s string type we can’t add a numeric value to it! Hence the Type Error.
    Another problem here is your max_year value is wrong based on the instructions provided earlier that are on the previous screen. If you have followed that correctly, your max_year value should be 4 digit number like 1971 or 2002 etc.
    You need to recheck this step.

If this one was done correctly then please share your notebook for us to have a look and help you out.

  1. The lambda functions will be explained in future lessons. For now, understand that this code helps us to extract all the years from the data list and find out the minimum and maximum year values. That is the earliest and latest year in the Data list.

  2. For the screenshot or file attachments (if they are taking time or hanging the screen), post your reply first. Then click on edit (it looks like a pencil) and then attach the screenshot/ file/ link etc.
    I am not sure why this is happening but it’s happening for me too.

2 Likes

for row in data:
fetch_year(row[0])
print(row[:3])

Is this right? Here’s my result for the previous question that I missed.
[‘September 25, 2020’, ‘Forest prison, Brussels’, ‘Belgium’]

Hi @kunal.shukla285

Check the instructions again from 'this task](Learn data science with Python and R projects).
It’s not wrong, but incomplete. You need to reassign the extracted year value back to row[0]. So that the first value in the list converts from “September 25, 2020” to just “2020”.

Just in case you have difficulty with the instruction and my response, this is what you are supposed to do in mission linked.

for row in data:
    # fetch just the year value (2020) and save it as the first value 
    # in the same list
    row[0] = fetch_year(row[0])

# not this
# print(row[:3])

# but this; display the first three rows of the data list 
# to see the effect of the `fetch_year` method
print(data[:3])

Just copy and paste the code into a different cell, run it and observe the difference between your (original) output and the output from this one.

2 Likes

I see the difference. I just get the year. I understand the syntax now based on what you said here " You need to reassign the extracted year value back to row[0]. So that the first value in the list converts from “September 25, 2020” to just “2020”." Thank you!

1 Like