Guided Project: Exploring Hacker News Posts : last step = TypeError: 'str' object is not callable

Screen Link:
https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/7/sorting-and-printing-values-from-a-list-of-lists

I got stuck because I found the instructions confusing, and so I tried to use the code provided in the solution

My Code:

# Sort the values and print the the 5 hours with the highest average comments.

print("Top 5 Hours for 'Ask HN' Comments")
for avg, hr in sorted_swap[:5]:
    print(
        "{}: {:.2f} average comments per post".format(
            dt.datetime.strptime(hr, "%H").strftime("%H:%M"),avg
        )
    )

Nevertheless, even if everything is identical, an error is occurring in (my) Jupyter Lab

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-170-5d87433cb98e> in <module>
      2     time = dt.datetime.strptime(hr, "%H").strftime("%H:%M")
      3     print("{hour}: {hour_avg:.2f} average comments per post".format(hour = time,
----> 4                                                                     hour_avg = avg))

TypeError: 'str' object is not callable

I struggled a lot to understand why but I didn’t come to any conclusion. Could someone explain to me why the code provided by data quest is not working in my Jupyter Lab environment ?
Could it be related to Python version ?

Many thanks

1 Like

Not able to figur out but this should solve it

  • Re-start kernel and re-run all cells!

image

Usually I do it (restart kernel and run all the cells) but thins time I didn’t try, because the code of all the other cells were working…

I followed your advice and it worked ! :slight_smile:

Thanks a lot for your swift reply and availability

1 Like

You have formated the time to a str object using .strftime (it changes a datetime to a string format). Leave it as a datetime object

time = dt.datetime.strptime(hr, "%H")

to access the hour you can use time.hour attribute

time = dt.datetime.strptime(hr, "%H")
print("{hour}: {hour_avg:.2f} average comments per post".format(hour = time.hour, hour_avg = avg))

Hi and thanks a lot for your answer.

Nevertheless, when I restarted the Kernel, it worked :

for avg, hr in sorted_swap[:5]:
print(
“{}: {:.2f} average comments per post”.format(
dt.datetime.strptime(hr, “%H”).strftime("%H:%M"),avg))

1900-01-01 15:00:00: 38.59 average comments per post
1900-01-01 02:00:00: 23.81 average comments per post
1900-01-01 20:00:00: 21.52 average comments per post
1900-01-01 16:00:00: 16.80 average comments per post
1900-01-01 21:00:00: 16.01 average comments per post

Without using strftime, the result is :

1900-01-01 15:00:00: 38.59 average comments per post
1900-01-01 02:00:00: 23.81 average comments per post
1900-01-01 20:00:00: 21.52 average comments per post
1900-01-01 16:00:00: 16.80 average comments per post
1900-01-01 21:00:00: 16.01 average comments per post

Which is not exactly what the instruction suggests

PS thanks for this debate, it’s helping me to reasoning on code :slightly_smiling_face:

FYI, In Jupyter Notebook (Python) I had used “str” as a variable for testing and got the "Type Error: ‘str’ " error. I learned that this is illegal in Python IF I am also using the string convert function str(). So, I removed the variable str and the error was persistent. I reloaded the program with a different name, same problem. On this site I found the advice to reset the Kernel and it worked. Thanks for the great advice from DishinGoyani !