Guided Project 2

Screen Link:
https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/3/extracting-ask-hn-and-show-hn-posts

My Code:

from csv import reader
opened_file = open('hacker_news.csv')
read_file = reader(opened_file)
hn = list(read_file)
hn = hn[:5]
print(hn)

# in order to isolate the headers we must extract:
headers = hn[0]
hn = hn[1:]
print(headers)
# headers are isolated and now we will display the first 5 rows
print(hn[:5])

# Create three empty lists
ask_posts = []
show_posts = []
other_posts = []

#loop Through data given title at index of 1 and append data
for row in hn:
    title = row[1]
    if title.lower().startswith('ask hn'):
        ask_posts.append(row)
    elif title.lower().startswith('show hn'):
        show_posts.append(row)
    else:
        other_posts.append(row)

# Check number of posts in each 
print('The number of ask posts equals', len(ask_posts))
print('The number of show posts equals', len(show_posts))
print('The number of other posts equals', len(other_posts))

What I expected to happen:
The correct number of ask_posts, show_posts and other_posts

What actually happened:

The number of ask posts equals 0
The number of show posts equals 0
The number of other posts equals 4

Any help will be greatly appreciated, Thank you!

The only thing wrong I see is that you are appending title to the lists. Try appending row and then post the output.

1 Like

Hello thank you for your response,

I append row instead of title, however still got the same incorrect output. I posted all of the code leading up to this line to hopefully help and see where my error is. Thank you again for replying and please let me know if you see my error now that all of my code is posted.

Your error is here:

When you do this you assign to hn only the five first rows. From now on in your whole program hn has only five rows.

If you want to display the first five rows you can do this:

print(hn[:5])
1 Like

It worked thank you so much!

1 Like