Guided Project: Explore Hacker News Post (Page 4)

Hi everyone, below is my code and the results of my guided project, is the figure correct? More importantly how to you verify if what you have coded is correct??? Appreciate any guidance. Thanks!

# Filter posts with 'ask hn' and 'show hn' into a list
ask_posts = []
show_posts = []
other_posts = []

for row in hn:
    title = row[1]
    title = title.lower()
    if title.startswith('ask hn'):
        ask_posts.append(row)
    elif title.startswith('show hn'):
        show_posts.append(row)
    else:
        other_posts.append(row)
# Calculate the average number of ask posts comments
total_ask_comments = 0
for row in ask_posts:
    comments = row[4]
    comments = int(comments)
    total_ask_comments += comments

avg_ask_comments = total_ask_comments/len(hn)
print(avg_ask_comments)

1.2180597014925374

# Calculate the average number of show posts comments
total_show_comments = 0
for row in show_posts:
    comments = row[4]
    comments = int(comments)
    total_show_comments += comments
    
avg_show_comments = total_show_comments / len(hn)
print (avg_show_comments)

0.5964179104477612

Hi! For both the avg_show_comments and avg_ask_comments, you are dividing by the size of the entire dataset. Since you’re trying to find the average number of comments within each category, you’ll want to divide instead by the size of each of the lists you created. Does that make sense?

When you’re working with the guided Jupyter projects in Dataquest, you can access the solution key by clicking the key icon above the notebook (see this post) There are some projects where choices you made while analyzing the data won’t exactly match up, but there should be enough similarity to give you a good idea if you’re on the right track.

1 Like