Hi all, I am doing the guided project 365: Exploring Hacker News Posts. While calculating average comments for specific post I run into the error. The dataquest solution code also returns the same error. Here is my code;
Calculate the average number of comments Ask HN
posts receive.
total_ask_comments = 0
for post in ask_posts:
num_comments = int(post[4])
total_ask_comments += num_comments
avg_ask_comments = total_ask_comments / len(ask_posts)
print(avg_ask_comments)
and the error:
ValueErrorTraceback (most recent call last)
in ()
3
4 for post in ask_posts:
----> 5 num_comments = int(post[4])
6 total_ask_comments += num_comments
7
ValueError: invalid literal for int() with base 10: ‘H’
anyone else encountered the same issue?
You cannot convert H
a string into an integer. Hence, the value error occurs.
2 Likes
I see now, the H comes from the mistake I made in the previous step. Thanks 
Thanks for the error readings. However, I am yet to figure the string character “H”. I will be glad for your help.
total_ask_comments = 0
for post in ask_posts:
num_comment = int(post[4])
total_ask_comments += num_comment
avg_ask_comments = total_ask_comments/len(ask_posts)
avg_ask_comments
Hi izabelcvetkovic, here is my previous code and I’m yet to spot the error. Kindly assist with the hint if you don’t mind
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1]
title = title.lower()
if title.startswith('ask hn'):
ask_posts.append(title)
elif title.startswith('show hn'):
show_posts.append(title)
else:
other_posts.append(title)
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
This looks good, I don’t understand, what is the error that you get?
I’ve checked my notebook for this project, but I can’t really remember how I initially got this error, sorry.
Thanks for the reply. The error message reads “Error invalid literal for int() with base 10: ‘h’”
@mo.yekini I’m not sure if this has been solved yet. I was having the same issue until I spotted the error when appending data to the three list-objects. You should reference the entire row instead of just title column values. See below.
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)
2 Likes
yea it really worked out @mo.yekini but an yet to understand while we append the row instead of title?
1 Like