Need Help with mistake in Guided Project 2 Hacker News

Hi!
Please, help me, I cannot figure out where is the problem? I am a newbie not only in Data Science but in the whole Programming , everything is too hard to combine in my head :smile:
Thank you and have a nice day.

Screen Link:

My Code:

import datetime as dt
result_list = []
for row in ask_posts:
    time = row[6]
    result_list.append(time)
    num_comments = row[4]
    num_comments = int(num_comments)
    result_list.append(num_comments)
print(result_list[:10])

counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
    datetime_str = row[0]
    datetime_hour = dt.datetime.strptime(datetime_str, "%m/%d/%Y %H:%M")

What I expected to happen:

What actually happened:

ValueError                                Traceback (most recent call last)
<ipython-input-55-a9be71d4ccb1> in <module>
     14 for row in result_list:
     15     datetime_str = row[0]
---> 16     datetime_hour = dt.datetime.strptime(datetime_str, "%m/%d/%Y %H:%M")
     17 
     18 

/usr/local/lib/python3.8/_strptime.py in _strptime_datetime(cls, data_string, format)
    566     """Return a class cls instance based on the input string and the
    567     format string."""
--> 568     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    569     tzname, gmtoff = tt[-2:]
    570     args = tt[:6] + (fraction,)

/usr/local/lib/python3.8/_strptime.py in _strptime(data_string, format)
    347     found = format_regex.match(data_string)
    348     if not found:
--> 349         raise ValueError("time data %r does not match format %r" %
    350                          (data_string, format))
    351     if len(data_string) != found.end():

ValueError: time data '8' does not match format '%m/%d/%Y %H:%M'                        

Hi sergibtrader

Welcome to the community!
Here result_list is a list of lists. Each item in it has 2 elements, time and num_comments. So for each iteration, we’ll create a list ([time, num_comments]) and append it to result_list.
So, your code should be like this:

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

Hope this helps.
Thanks.

1 Like

Thank you very much.

1 Like