attempts_per_year = 
min_year = min(data, key=lambda x: x)
max_year = max(data, key=lambda x: x)
for y in range(min_year, max_year + 1):
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)
3 max_year = max(data, key=lambda x: x)
----> 4 for y in range(min_year, max_year + 1):
TypeError: can only concatenate str (not "int") to str
- Copy and paste the given codes into a new code cell.
- Create a list,
attempts_per_year, whose elements all look like
- 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
- 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):
Could you please print max_year and post the output.
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.
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.
- 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.
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.
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.
for row in data:
Is this right? Here’s my result for the previous question that I missed.
[‘September 25, 2020’, ‘Forest prison, Brussels’, ‘Belgium’]
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. 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 = fetch_year(row)
# not this
# but this; display the first three rows of the data list
# to see the effect of the `fetch_year` method
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.
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. So that the first value in the list converts from “September 25, 2020” to just “2020”." Thank you!