Ask_posts and show_posts return 0

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 #importing the csv
opened_file = open('hacker_news.csv')
hn = list(opened_file) #assigning variable hn to the list
headers = hn[0] #Isolating the headers row
hn = hn[1:] #Removing headers row

ask_posts = []
show_posts = []
other_posts = []

for row in hn:
    title = row[1]
    if title.lower().startswith("ask hn"):
        ask_posts.append(title)
    elif title.lower().startswith("show hn"):
        show_posts.append(title)
    else:
        other_posts.append(title)
        
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))

What actually happened:

These are the results that I am getting:

0
0
20100

In the answer below I’ll give some clues and leave you with some questions for you to answer by yourself. Let me know if you need more help.

It seems all titles are being appended to other_posts, which in turns means that every title is missing both the conditions (title.lower().startswith("ask hn") and title.lower().startswith("show hn")).

Why would this be?

Try checking what title looks like. You can run the following code (added a line to yours):

from csv import reader #importing the csv
opened_file = open('hacker_news.csv')
hn = list(opened_file) #assigning variable hn to the list
headers = hn[0] #Isolating the headers row
hn = hn[1:] #Removing headers row

ask_posts = []
show_posts = []
other_posts = []

for row in hn:
    title = row[1]
    if title.lower().startswith("ask hn"):
        ask_posts.append(title)
    elif title.lower().startswith("show hn"):
        show_posts.append(title)
    else:
        other_posts.append(title)

print(title) # <-------- Added this line. See comments below.
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))

The variable title will hold the last value in the iteration, so you can at least inspect this value.

Does title look wrong to you?

Since title comes from row, try printing row as well. Does row look wrong to you?

Since row comes from hn, try inspecting a few of the elements of hn. Do they look wrong to you? Why would they look like this? What was done differently from the solution notebook or other missions?

I hope this helps.

1 Like

I believe I got it. Check it out, please. Thank you.

from csv import reader #importing the csv
opened_file = open('hacker_news.csv')
read_file = reader(opened_file)
hn = list(read_file) #assigning variable hn to the list
headers = hn[0] #Isolating the headers row
hn = hn[1:] #Removing headers row


ask_posts = []
show_posts = []
other_posts = []

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)

print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
2 Likes

Looks good.

Hi,

I have hit the same problem of having all elements assigned to “other_posts”. I have read your explanations several times, but I still do not understand why we need to use “append(row)” instead of “append(title)”.

I tried printing the title as per your advice, and it returned the title of the post, so I don’t quite understand what this added line (print(title)) should explain.

Sorry if this is a dumb question, but I have zero coding experience and doing my first steps. Your help would be much appreciated! Thanks!

Nurbek

I think it’s better if you ask this in a new topic.

Please include your code and try to be precise in your question(s).

row in this case means post, so we are appending entire post(with title includes lowercase ask hn, for example)

Hi Bruno,
I’ve also checked through as you have guided us but I still don’t clearly get the reason why we have to use a row.
Thanks

1 Like

You could have just answered these gentlemen’s questions if you can, instead of directing them to start a new topic, this creates an unnecessary long process to get help.


Basically at number 3 under instructions, if tells you to append the row to ask_posts, show_posts and other_posts. So instead of “ask_posts.append(title)” it should be “ask_posts.append(row)”. In this case you will be adding the whole row or list to the “ask_posts” posts and not just the posts that starts with “ask hn”. Hope this helps

ask_posts =
show_posts =
other_posts =
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)
print(“number of ‘ask post’ is”, len(ask_posts))
print(“number of ‘show post’ is”, len(show_posts))
print (“number of ‘other posts’ is”, len(other_posts))

Hi Bruno,
I have a similar problem, here is my code. I understand why we need to append the whole row, and not just the title, but still, I come up with len of 0 for ask_posts and show_posts. Could you give me a few more hints?
ask_posts =
show_posts =
other_posts =
for row in hn:
title = str(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)

ask_posts_num = len(ask_posts)
show_posts_num =len(show_posts)
other_posts_num = len(other_posts)

print(ask_posts_num)
print(show_posts_num)
print(other_posts_num)

Please ask this in a new topic. Also include your notebook.