Hacker News - Help is needed - Stuck at the end

Screen Link:

My Code:

"the issue is within the last 6-7 lines of codes ..

import datetime as dt

result_list = []

for row in ask_posts:
    created_at = row[6]
    num_of_comments = float(row[4])
    tuple = (created_at, num_of_comments)

counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
    date = row[0]
    hour = dt.datetime.strptime(date, '%m/%d/%Y %H:%M')
    hour = hour.strftime('%H')
    if hour not in counts_by_hour:
        counts_by_hour[hour] = 1
        comments_by_hour[hour] = row[1]
        counts_by_hour[hour] += 1
        comments_by_hour[hour] += row[1] 

ask_list = []
show_list = []

for key in counts_by_hour:
    ask_list.append([key, counts_by_hour[key]])
for key in comments_by_hour:
    show_list.append([key, comments_by_hour[key]])

final_list_now = []
for row in counts_by_hour:
    ask_value = int(row[1])
    for here in comments_by_hour:
        hour_now = row[0]
        comment_value = int(row[1])
        avg_per_post = comment_value / ask_value(the result is saying it devided by ZERO)

What I expected to happen: To make it work and then to move it for a different list i guess

What actually happened: error

Help is needed,Thanks

If that’s the error then that means your ask_value is 0 somewhere and it shouldn’t be.

If ask_value is 0, that means your row[1] is 0.

You get row from counts_by_hour. But, counts_by_hour doesn’t have any rows. It’s a dictionary. So, row would be a key in counts_by_hour.

So, you need to think about your code logic carefully and ask yourself why you are using row[1]?

Your keys in counts_by_hour are all strings. If you have

row = "10"

then row[1] would result in a 0 and that’s what eventually causes the error. So, think about why you need row[1] and that should help you figure out the problem as well.

Thanks somehow I have solved it earlier :slight_smile:

but i have some last issue i need to solve to finish the task of the Hacker news

best_5 = sorted_swap[:4]
for row in best_5:
    hour = row[1]
    Hour = dt.datetime.strptime(hour, '%H')
    HOUR = Hour.strftime('%H:00')
    average = float(row[0])
    string = "{}: {:.2f} average comments per post"
    format_string = string.format(average, HOUR)

ValueError                                Traceback (most recent call last)
<ipython-input-8-ea8a46b10a07> in <module>
      9     average = float(row[0])
     10     string = "{}: {:.2f} average comments per post"
---> 11     format_string = string.format(average, HOUR)
     12     print(format_string)

ValueError: Unknown format code 'f' for object of type 'str' '''

Here to be honest i tried few things which didn't work...

This is telling us that python is trying to format a str where it’s expecting a float. I think that’s because average is a float and HOUR is a string. Yet, in these lines of code:

Python is expecting a str and then a float…but it’s getting float and then a str. Try switching the order of your variables average and HOUR inside string.format() and see if that makes a difference.

1 Like

Yes it was stupid how i didnt notice , i guess this is where your brain is tired or overwhelmed

No worries, it happens to us all! Did this fix your error? Is it working as expected now?

Yes i fixed it few days ago thanks
by the way do you aware the software has some issue when clicking ctrl +Z and then somehow it messes up the codes in the practice stage and even when im trying to click restore the codes it dont work?

i even tried to clean the cookies etc in my browser but it didnt work at all ,so im wondering if there is some solution for this issue so I will be able to continue with the practicing

My apologies for seeing this so late…

I’m not sure I understand exactly what you are experiencing here. Since this seems to be a new issue, can you create a new topic with links to the screen you’re working on in order to troubleshoot it better?

Also, since it appears your original question has been resolved here, can you select one of the above responses as being the Solution to your post? Just look for the little checkbox next to “Solution” below the response that solved your original problem.

This is the link for the issue but till now the support didnt help me almost a week if not more than that…
If you know of have some solution to the issue will be happy to hear some advice…