Getting error in the last step of the Hacker_news guided project

Screen Link:

My Code:

for hour in sorted_swap[:5]:
    hour = dt.datetime.strptime(hour[1], "%H")
    hour = hour - dt.timedelta(hours = 1)
    hour = hour.strftime("%H:%M")
    print(
            "{}: {:.2f} average comments per post ".format(
            dt.datetime.strptime(hour, "%H").strftime("%H:%M")
        )
    )

What actually happened: Im stuck at this piece of code. I want to subtract 1 hour from EST time and not sure what should be passed in .format()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-67-692188605763> in <module>
      5     print(
      6             "{}: {:.2f} average comments per post ".format(
----> 7             dt.datetime.strptime(hour, "%H").strftime("%H:%M")
      8         )
      9     )

/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)
    350                          (data_string, format))
    351     if len(data_string) != found.end():
--> 352         raise ValueError("unconverted data remains: %s" %
    353                           data_string[found.end():])
    354 

ValueError: unconverted data remains: :00

Please guide me where I'm going wrong

The below given points might help you.

%H : The hour (24-hour clock) [00,23]; leading zeros are permitted but not required.

%I : The hour (12-hour clock) [01,12]; leading zeros are permitted but not required.

%M : The minute [00,59]; leading zeros are permitted but not required.

%p : The locale’s equivalent of a.m or p.m.

If you want to read more check here

I hope you will sort this out with this hint.

1 Like