Kunal's Guided Project: Prison Break 10/14

Screen Link:

My Code:

# Instruction 1 - for each row in data
for row in data:
    for ya in attempts_per_year: # Instruction 2 - nothing to do here
        # Instruction 3 - assign the year value in ya to y
        ya[0] = y[0]
        if row[0] == y:
            ya[1] += 1

# Instruction 4 - print the results
print(attempts_per_year)

What I expected to happen: Not sure, but something is supposed to print.

What actually happened:

[]

I’m confused with this one as well. Don’t why I am struggling so much, but yeah, I’ve got to ask. What’s indexing again? I thought it was just where you bracket a corresponding row to a variable, which is then assign to a value.

1 Like

How do I post the screen link too?

Hi @kunal.shukla285, your question seems confusing in a way but from your code, I can deduce that you didn’t follow the instruction given well here

This isn’t correct as you’re not supposed to add [0] in front of ya, it just supposed to be this

So probably check back and see where you missed it.

I made the appropriate changes and still get an empty bracket as an answer:

Instruction 1 - for each row in data

for row in data:
for ya in attempts_per_year: # Instruction 2 - nothing to do here
# Instruction 3 - assign the year value in ya to y
y = ya[0]
if row[0] == y:
ya[1] += 1

Instruction 4 - print the results

print(attempts_per_year)

HI @kunal.shukla285, Kindly Check your indentations

What’s wrong with my indentations? I reentered everything and the code is still not working. Can you show me what the indentations and code are supposed to look like? Then, it’ll make sense to me. I keep getting the same empty bracket as an answer.

can you screenshot your code and let me see?

The website won’t let me upload or paste my screenshot

with your keyboard click on fn+windows button+printscreen all together at once

I clicked Fn+windows+ spacebar + prt Sc, but nothing happened. What did i do wrong? The screen was highlighted for a second but that’s it.

Yeah, I screenshotted the page and was able to paste it elsewhere, but not on this reply section.

I tried uploading it again or pasting it. It didn’t work.

refresh your page and paste the screenshot

I refreshed. That didn’t work.

1 Like

Hi @kunal.shukla285,

The issue is with this line of your code:

ya[0] = y[0]

What you actually need to do here is to create a new variable y inside the inner for-loop and assign to it at each iteration the year of attempt. So that line of code will be:

y = ya[0]

Before implementing this amendment, though, please run all the previous code cells in the order they appear in your notebook, because it can be that you had re-written some variables by mistake. Then, try to make this amendment and let me know if it works.

As for your screenshot, there are current issues with uploading images to the posts in the Community that are being fixed now.

Anyway, in the future, @kunal.shukla285 and @OlutokiJohn, I would actually suggest to both of you to avoid sharing screenshots. The most efficient way is to share your code itself, so that other people can copy-paste it, run it on the platform or on their local machines, and hopefully give you some suggestions.

1 Like

Hi Elena,

Thank you! I realize from the beginning that I didn’t understand the code because I didn’t understand the problem. I think the problem is asking me how many prison break attempts there were each year? Can you explain what the problem is asking?

I took note of your code, and am still having issues with mine.

# Instruction 1 - for each row in data
for row in data:
    for ya in attempts_per_year:
        y = ya[0]
        if row[0] == y:
            ya[1] += 1
        # Instruction 2 - nothing to do here
        # Instruction 3 - assign the year value in ya to y

# Instruction 4 - print the results
print(attempts_per_year)
1 Like

Hi Kunal,

You’re absolutely right, here we’re trying to find the number of attempts per year.

The code in your last post is absolutely correct, and you don’t have any problem with the indentation. It’s clear that the issue derives not from this piece of code but from your previous code.

Could you please share your full code so far cell-by-cell? Starting from from helper import *. It would be even better if you can upload here your ipynb file, but I’m afraid that there are current technical issues with it on the platform, so sharing your code cell-by-cell is ok.

1 Like

did you do this?

try re-runing all your codes again

1 Like
from helper import *

url = ('https://en.wikipedia.org/wiki/List_of_helicopter_prison_escapes')

data = data_from_url(url)

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

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


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


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)
print(max_year)


# Instruction 1 - for each row in data
for row in data:
    for ya in attempts_per_year:
        y = ya[0]
        if row[0] == y:
            ya[1] += 1
        # Instruction 2 - nothing to do here
        # Instruction 3 - assign the year value in ya to y

# Instruction 4 - print the results
print(attempts_per_year)

This is all my code? What are your thoughts?

1 Like

I see the following issues:

  1. In this piece of code:
index = 0
for row in data[1:]:

you should start from the first row, not from the second one. I mean, remove [1:]

  1. Uncomment this part of your code:
# for y in range(min_year, max_year + 1):
   # years.append(y)

You need this part of code to create the years list.

  1. Also, you actually forgot to initialize the years list. So, right before the piece of code above (the one that you need to uncomment), initialize the empty list: years = [].

After these amendments, everything should work fine. Please let me know