Feedback for Guided Project: Exploring Hacker News Posts Final Step

Hello,

I would like to get feedback on my code for the last step in the Guided Project: Exploring Hacker News Posts which can be found here.

See below:

print("Top 5 Hours for Ask Posts Comments")
top_five_hours = "{hour}: {number:.2f} average comments per post"
for row in sorted_swap[:5]:
    average_comments = row[0]
    topfive_hour = row[1]
    topfive_hour_dt = dt.datetime.strptime(topfive_hour, "%H")
    topfive_hour_str = topfive_hour_dt.strftime("%H:%M")
    topfive_format = top_five_hours.format(hour = topfive_hour_str, number = average_comments)
    print(topfive_format)

What I expected to happen: I arrived at the expected result, but my code is a bit lengthier. This module has been especially challenging, and the way I coded my solution is the only way I understand these topics currently. I had no idea that I could arrive at the same result with a shorter code.

Was what I did also correct and if I keep doing this will I run into problems in the future.

1 Like

Hi @rudicandaza,

In my limited knowledge what you did can also be done by replacing those variables into the last line of code before print(). That was, maybe we can right less number of lines of code, but I think it will make it very complex to read and understand for a beginner like me. You can write the topfive_hour_str and topfive_format in one line. But it will be complex and hard to understand.

The way you have written the code makes it very simple to understand and thus reduces any chances of errors that might arise. So, in my opinion, it is good. Some experts might know a shortcut way though which I’m not sure.

1 Like

Hello @jithins123,

Thank you for the feedback. That is reassuring. For now I’ll probably use this way until I learn how to do code in a cleaner manner.

1 Like