Python Project: Exploring Hacker News

Hey Everyone! Need some help with this. Sorry this might be stupid for you guys but I was trying hard to figure this out.
This is my code.

total_ask_comments = 0

for comment in ask_posts:
num_comment = int(comment[4])
total_ask_commtents += num_comment

avg_ask_comments = total_ask_comments / len(ask_posts)
print(avg_ask_comments)

this is the error
“ZeroDivisionError: division by zero”

So now, when I review the previous cell and run this code:
ask_posts =
show_posts =
other_posts =

for row in hn:
title = row[1]
if title.lower().startswith(‘ask hn’):
ask_posts.append(row)
elif title.lower().startswith(‘show hn’):
show_posts.append(row)
else:
other_posts.append(row)

print(ask_posts)

the ask_posts is .

What did I miss here? Or what was wrong in my code?

Thanks!

1 Like

Hi @Euy,

The code from your previous cell is correct and should work. However, it seems that you have some issues with the hn variable. Since I cannot see your initial code, I’d suggest you to re-run the main part of it:

import csv
import datetime as dt
opened_file = open('hacker_news.csv')
read_file = csv.reader(opened_file)
hn = list(read_file)
headers = hn[0]
hn = hn[1:]

and then re-run your code (the cell where you create the ask_posts list). It should work now.