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 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!
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)