Question:
I am not getting what I’m doing wrong. Can someone please review my code based on question 5 - 8 ?
The first 4 questions were clear.
Screen Link: https://app.dataquest.io/m/404/guided-project%3A-exploring-hacker-news-posts/5/finding-the-amount-of-ask-posts-and-comments-by-hour-created
Your Code:
import datetime as dt
result_list = []
for element in ask_posts:
created_at = element[6]
comments_post = element[4]
result_list.append([created_at, int(comments_post)])
result_list[:5]
Output:
[['8/16/2016 9:55', 6],
['11/22/2015 13:43', 29],
['5/2/2016 10:14', 1],
['8/2/2016 14:20', 3],
['10/15/2015 16:38', 17]]
counts_by_hour = {}
comments_by_hour = {}
format_string = "%m/%d/%Y %H:%M"
for row in result_list:
hour = row[0]
hour_object = dt.datetime.strptime(hour, format_string)
hour = dt.datetime.strftime("%H:%M")
if hour not in counts_by_hour.keys():
counts_by_hour.key == 1
comments_by_hour.key == row[1]
if hour in counts_by_hour.keys():
counts_by_hour.value += 1
comments_by_hour += row[1]
1 Like
Sahil
March 24, 2020, 7:16am
#2
Hi @LuukvanVliet ,
The first error in your code:
----> 8 hour = dt.datetime.strftime("%H:%M")
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'
can be fixed by replacing dt.datetime.strftime("%H:%M")
with hour_object.strftime("%H:%M")
. However, looking at the errors associated with dictionaries, I would recommend you to go through the Dictionaries and Frequency Tables mission once again.
I believe that this post will help you to understand the Dictionary syntax:
Introduction
This article will help you understand the concept of creating new key-value pairs in an empty dictionary using dictionary['key'] = value syntax.
What is the cause of the confusion?
If the dictionary is empty, how can we do dictionary['key'] = value? You may think that in order to use a key in the syntax dictionary['key'] = value, it should exist in the dictionary. The error below shows us why you might think that:
>>> dictionary = {}
>>> dictionary['non_existent_key']
Traceback (m…
Best,
Sahil