Guided Project: Exploring Hacker News Posts

Hi I am recently doing the Guided Project: Exploring Hacker News Posts, and I am stuck on some issue: where when I tried to convert the num_comment to integer, it says invalid literal for int() with base 10: ‘H’. I have no idea how to fix that…
And do you possibly have the solution guide too?
Thanks a lot!!

1 Like

Hi! Already completed that project and would be glad to help you. Could you post you current code please? Don’t forget to use the code snippet tool: </> Thanks!

total_ask_comments = 0
for row in ask_posts:
num_comments = int(row[4])
total_ask_comments.append(num_comments)
avg_ask_comments = sum(total_ask_comments) / len(ask_posts)
print(avg_ask_comments)

total_show_comments = 0
for row in show_posts:
num_comments = int(row[4])
total_show_comments.append(num_comments)
avg_show_comments = sum(total_show_comments) / len(show_posts)
print(avg_show_comments)

This is where I went wrong… and I’m not sure what code snippet tool is.

Hi,

You’re algorithm / logic is correct but it seems you’re trying to use the append method to increment the counter variable: total_ask comments, that is initialized as an integer and this is resulting in an error. In python, the append() method “appends or inserts” an element to the end of the list. If you want to know more about the append method: https://docs.python.org/3.5/library/array.html?highlight=append#array.array.append

In the total_ask_comments you want to sum the total posts of all rows so, try this instead:

total_ask_comments = 0
for row in ask_posts:
    total_ask_comments += int(row[4])
    avg_ask_comments = sum(total_ask_comments) / len(ask_posts)
print(avg_ask_comments)

You should replicate the solution for the avg_show_comments block

Let me know if it worked.
Cheers,
Ricardo

It still has the same problem… It doesn’t work

from csv import reader
opened_file = open(“hacker_news.csv”)
read_file = reader(opened_file)
hn = list(read_file)

headers = hn[0]
hn = hn[1:]
print(headers)
print(hn[:5])

ask_posts =
show_posts =
other_posts =

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

print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))

total_ask_comments = 0
for row in ask_posts:
total_ask_comments += int(row[4])
avg_ask_comments = sum(total_ask_comments) / len(ask_posts)
print(avg_ask_comments)

total_show_comments = 0
for row in show_posts:
total_show_comments += int(row[4])
avg_show_comments = sum(total_show_comments) / len(show_posts)
print(avg_show_comments)

That’s what I have done so far with the code. Hope it helps you to determine the issue? Thank you so much.

Hi there and welcome!

I believe the reason you’re getting the error is when you used the following to make your show_posts, ask_posts, and other_posts lists:

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

When I inspected the ask_posts list, for example, it only contains the titles of the posts, like so:

['Ask HN: How to improve my personal website?',
 'Ask HN: Am I the only one outraged by Twitter shutting down share counts?',
 'Ask HN: Aby recent changes to CSS that broke mobile?',
 'Ask HN: Looking for Employee #3 How do I do it?']

When you loop through this list of strings, row[4] returns the 4th index of the string – which happens to be 'H'. Since you can’t turn convert that to an int, it throws up the error.

To fix this, instead of appending title to each of your lists, you need to append the row. Then you will have a list of lists, and row[4] will retrieve the number of comments.

I hope this helps!

As far as formatting the code in your posts, the code snippet tool is here:


You can also do 3 backticks before and after your pasted code so that it formats nicely (which is what I did when I copied the larger chunks in this message).
image

9 Likes

Yes thanks it helps!! One more issue about the project, it’s to convert the date time into hour scale…
I have no idea how it works. I have looked through the previous missions but still not a clue…

‘’’
import datetime as dt
result_list = [,]
for row in ask_posts:
created_at = row[6]
result_list[0] = created_at
result_list[1] = int(row[4])

counts_by_hour = {}
comments_by_hour = {}
for row in result_list:
hour = result_list[0].scrptime("%H")

‘’’

This is what I did in the code.

scrptime() seems to be a typo? --> strptime()

Hi again @ahsiu611. I’m having trouble understanding your code because it didn’t format correctly. It would help if you could edit your snippet by highlighting the code and pressing the code snippet tool. Your code for creating result_list is different than how I did it and I wanted to make sure that wasn’t part of the issue.

If result_list is okay, then the next issue to tackle is getting the hours. You’ll need to convert the timestamp to a datetime object first with datetime.strftime() and then you’ll be able to get the hour with datetime.strptime(). If you’ve forgotten how to do that, I would recommend revisiting the Dates and Times in Python mission since there’s a lot of great info in there.

Hi even I appended the raw, it still does not work,

Please see my code attached.

Regards

Peter

I was also stuck here. Thank you.

Greetings.
My sum(total_ask_comments) show error

TypeError “int” is not iterable.
What could be the cause?

Hello @onyidojohn,

This post on TypeError: 'x' object is not callable should be helpful.

Happy Learning!

1 Like

Hi @onyidojohn,

In this code:

total_ask_comments = 0
for row in ask_posts:
    total_ask_comments += int(row[4])
    avg_ask_comments = sum(total_ask_comments) / len(ask_posts)
print(avg_ask_comments)

total_ask_comments is an integer. We can only pass an iterable (tuple, list, etc.) to the sum function. That is why you are getting an error. Here is a simple way to reproduce it:

sum(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

Best,
Sahil

1 Like