Hacker News Project - help needed

Am working on Hacker News guided project. In screen 6/8, my averages do not calculate; they all come out to 2.0 and I do not understand what I am doing wrong. The point of confusion for me is that there are two dictionaries; am I supposed to iterate over both of these? The link to the mission and my code is below; any advice would be appreciated. Thanks!

https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/6/calculating-the-average-number-of-comments-for-ask-hn-posts-by-hour

I couldn’t see your mission or code when I clicked on the link it brings mine up but here is the code that i used. hope this helps

avg_by_hour =

for hour in comments_by_hour:
avg_by_hour.append([hour, comments_by_hour[hour] / counts_by_hour[hour]])

print(avg_by_hour)

1 Like

This is exactly the code I used. Still not sure why I’m getting the averages I have, but thank you for your response!

Hi @desideriojeremy. Another user was also getting 2.0 for the averages a while back, and it looked like the cause was in the creation of the count_by_hour and comments_by_hour dictionaries. They didn’t happen to post again to see how it was resolved though. It’s hard to say what specifically is causing your error without more information, but that would be a good place to start.

If you’re not able to figure it out, it would help if you share your notebook file so that members of the community can have a closer look. And if you do happen to discover it, please share!

Hi @april.g. Thank you for your response. I am unable for some reason to share the Notebook file, so I have pasted my code below. Any thoughts would be appreciated.

Input:

import datetime as dt

result_list =

for row in ask_posts:
created_at = row[6]
num_comments = int(row[4])
result_list.append([created_at, num_comments])

counts_by_hour = {}
comments_by_hour = {}

date_format = ‘%m/%d/%Y %H:%M’
for row in result_list:
dt_string = row[0]
dt_string = dt.datetime.strptime(dt_string, date_format)
row[0] = dt_string

dt_hour = dt_string.strftime('%H')

if dt_hour not in counts_by_hour:
    counts_by_hour[dt_hour] = 1
    comments_by_hour[dt_hour] = num_comments

else:
    counts_by_hour[dt_hour] += 1
    comments_by_hour[dt_hour] += num_comments

avg_comments =
for dt_hour in comments_by_hour:
avg_by_hour = comments_by_hour[dt_hour]/counts_by_hour[dt_hour]
avg_comments.append([dt_hour, avg_by_hour])
print(avg_comments)

Output:

[[‘17’, 2.0], [‘06’, 2.0], [‘07’, 2.0], [‘13’, 2.0], [‘16’, 2.0], [‘01’, 2.0], [‘08’, 2.0], [‘14’, 2.0], [‘10’, 2.0], [‘19’, 2.0], [‘21’, 2.0], [‘00’, 2.0], [‘12’, 2.0], [‘15’, 2.0], [‘04’, 2.0], [‘03’, 2.0], [‘05’, 2.0], [‘09’, 2.0], [‘02’, 2.0], [‘18’, 2.0], [‘20’, 2.0], [‘11’, 2.0], [‘23’, 2.0], [‘22’, 2.0]]

Sorry you weren’t able to attach the notebook file, but there was enough here to be able to spot the problem, so thanks!

The problem shows up from the if/else statement with num_comments:

    if dt_hour not in counts_by_hour:
        counts_by_hour[dt_hour] = 1
        comments_by_hour[dt_hour] = num_comments
    else:
        counts_by_hour[dt_hour] += 1
        comments_by_hour[dt_hour] += num_comments

In the current loop where the if/else is located, num_comments isn’t defined, so it’s using the last value of num_comments from the first loop, which happens to be 2. Since every value is 2, that’s why you’re seeing all the averages as 2.

To fix it, you need to define num_comments in the 2nd loop from result_list, just like you did for dt_string.

I hope that helps clear things up!

Thanks April! This worked.

hi, hope this could help
avg_by_hour =
for hour in comments_by_hour:
avg_by_hour.append([hour,comments_by_hour[hour] / counts_by_hour[hour]])

print(avg_by_hour)

output:
[[‘21’, 16.009174311926607], [‘10’, 13.440677966101696], [‘08’, 10.25], [‘02’, 23.810344827586206], [‘14’, 13.233644859813085], [‘12’, 9.41095890410959], [‘23’, 7.985294117647059], [‘20’, 21.525], [‘17’, 11.46], [‘19’, 10.8], [‘03’, 7.796296296296297], [‘07’, 7.852941176470588], [‘09’, 5.5777777777777775], [‘11’, 11.051724137931034], [‘01’, 11.383333333333333], [‘06’, 9.022727272727273], [‘18’, 13.20183486238532], [‘00’, 8.127272727272727], [‘15’, 38.5948275862069], [‘04’, 7.170212765957447], [‘22’, 6.746478873239437], [‘13’, 14.741176470588234], [‘05’, 10.08695652173913], [‘16’, 16.796296296296298]]

1 Like