Guided Project: Exploring Hacker News Posts (step 4) I have different results from the solution notebook


I seem to have different results from the solution notebook. which seems to show

avg ask comments:

avg show comments:

Screen Link:
Step 4

My Code:

total_ask_comments = 0
for post in ask_posts:
    n =int(row[4])
    total_ask_comments +=n
print("Total Posts for Ask Posts: " + str(total_ask_comments))
avg_ask_comments = total_ask_comments/len(ask_posts)
print("Avg Posts for Ask Posts: " + str(avg_ask_comments))

total_show_comments = 0
for post in show_posts:
    n =int(row[4])
    total_show_comments +=n
print("Avg Posts for Show Posts: " + str(total_show_comments))
avg_show_comments = total_show_comments/len(show_posts)
print("Avg Posts for Show Posts: " + str(avg_show_comments))

What I expected to happen:

What actually happened:

Paste output/error here

Total Posts for Ask Posts: 101152
Avg Posts for Ask Posts: 58.0
Avg Posts for Show Posts: 67396
Avg Posts for Show Posts: 58.0

Hi @gayletan. In both of your loops, you have post as your iteration variable, but when getting the value for the comments, you’re using row. The value of row[4] is going to be the same every time because it’s probably using the last value from a previous loop. Try changing it from row[4] to post[4] and see what happens.

1 Like

hi April,

thanks for the quick reply, silly mistake on my end.

1 Like