Exploring Hacker News Posts - TypeError: sorted() got an unexpected keyword argument 'reverse'

I am not able to get past the sorted() in the exercise in Sorting and Printing Values from a List of Lists of Hacker News guided project.
It throws the following error:
TypeError: sorted()got an unexpected keyword argument 'reverse'

I have attached my project file for reference. Please help and also, kindly excuse the comments in the markdown cells, I’m still learning. :slight_smile:

HackerNews.ipynb (13.4 KB)

Click here to view the jupyter notebook file in a new tab

1 Like

Hi @menon.aj,

I ran your notebook from the start and the error I received was:
---> 13 time = dt.datetime.strftime(time, "%H:%M")
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

This is because the value stored in time is a string. Instead of converting it to a datetime object and then back to a string as shown in solution, I would recommend you to just use the string as it is like this:

for avg, hr in sorted_swap[:5]:
    print(
        "{}:00 {:.2f} average comments per post".format(hr, avg)
        )
15:00 38.59 average comments per post
02:00 23.81 average comments per post
20:00 21.52 average comments per post
16:00 16.80 average comments per post
21:00 16.01 average comments per post

Best,
Sahil

2 Likes

OR:

for row in sorted_swap[:5]:
avg_comments = row[0]
ptime = row[1]
ptime_dt = dt.datetime.strptime(ptime, “%H”)
ptime_dt_h = ptime_dt.strftime("%H:%M")

result = "{}: {:.2f} average comments per post".format(ptime_dt_h ,avg_comments)

print(result)
1 Like