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