Code Problem on Guided Project: Prison Break

Screen Link:
Guided Project: Prison Break β€” Extracting The Year | Dataquest

My Code:

Replace this line with your code

What I expected to happen:

What actually happened:

Replace this line with the output/error

Hi @skkirkes

I was reading your question. Is this the entire code?

I am not sure. I am very new to python and it has been challenging. I was trying to get information into the community chat yesterday and it was not clear exactly what I need to include. Is there a tutorial for me to rear or show me what to include?

I believe this is my code that I am having trouble with:

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

print(data[:3])

This is my error message:

IndexError Traceback (most recent call last)
in
1 for row in data:
----> 2 date = fetch_year(row[0])
3 row[0] = date
4
5 print(data[:3])

IndexError: list index out of range

I am working on Prison Break Guided Project and I have the answer key. It appears to match what I have, but I am not getting the result they have.

I am confused on when to use certain characters (i.e. parentheses, equal signs/double equal signs, brackets, etc.). I am trying to stay with it, but it is hard.

Any help is much appreciated.

Thank you, Scott

2 Likes

Hi Scott,

Welcome to the Community!

As for the first part of your question:

I was trying to get information into the community chat yesterday and it was not clear exactly what I need to include. Is there a tutorial for me to rear or show me what to include?

You can find useful this post:

Happy learning! :nerd_face:

Hi @skkirkes

I felt the same way. The different signs or syntax are confusing in the beginning. Give yourself some grace and be proud of yourself for starting a new programming language which is not an easy feat. Here is a dataquest class that will help you with the signs and syntax.

As for the signs and symbols, each sign can have multiple use cases. I included a link to a full dataquest class that goes into details.

Click on the triangle bullets below to view additional details about the signs you asked about :

Parentheses

Use parentheses to start a function like in your code below. You used the fetch_year() function. To call a function you must use ( ).
fetch_year( )

Inside the parethesis you can put your arguments that the function needs to work. In this case you must include a date value so the function, fetch_year() can return a year.
You can read more about functions here.

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

print(data[:3]) # used parentheses to call the print function and inside the () you passed the arguments to tell it what to print.  In this case you wanted to print the values in your data list.

There are many more use cases that you will learn as you continue to study Python or here

Equal Signs

You used = sign to assign the value from fetch_year(row[0]) to date. Once the fetch_year () function returns a year value that year value will be assigned and stored in date.

You also used = sign to assign the date value back to row[0]

Basically, you used the equal sign to assign a value (right side) to a variable (left side of equal sign)

for row in data:
date = fetch_year(row[0])  # the value returned from fetch_year() will be assigned to **date** variable
row[0] = date

print(data[:3])

There are many more use cases that you can study here

Double equal signs

I know your code did not include == but what if you needed to compare two values? You can use == sign for that.

for row in data:
date = fetch_year(row[0])
row[0] = date
if date == 1972:  # this is made up and not part of the project but here you can use == to compare if the date variable is equal to 1972
     print(data[:3])

There are many more use cases that you can study here

Brackets

You used [ ] in code below to subscript your data list. You used row[0] to only pull out the value at the 0 index of the row

for row in data:
date = fetch_year(row[0]) # you only want the 1st value of the row at index 0.  row[0]
row[0] = date

print(data[:3]) # you only want to print up to the 3rd value of the data list:     **data[:3]**

There are many more use cases that you can study here

I hope this helps.

As for the error code, your data list may be the problem.

IndexError: list index out of range

Usually index out of range means there is not a value at that index position that it is trying to read and since you are only reading to the 3rd value - data[:3] your list may be the problem.

Do you mind sharing your first lines code where you setup the data list?

Again, I started out very confused but dataquest classes really helped me understand the fundamentals and by practicing I started to catch on. It just takes time.

Glad you are a part of the community. :grinning:

2 Likes

Sorry for the late response. Thank you for taking the time to put this together. I really appreciate it. I started the whole project over and was able to finish it without the above-mentioned errors. I am trying to stay with it, but it hard for a complete novice to pick-up. Thanks again.

1 Like

Hi @skkirkes,

Great job sticking with it.

1 Like