Hi, I got stuck here and I can’t find any solution to handle the problem. I tried to implement solutions from https://community.dataquest.io/t/guided-project-exploring-hacker-news-posts/8764/4;
My 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_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)
What actually happened:
3 for row in ask_posts:
----> 4 total_ask_comments += int(row[4])
5
6 avg_ask_comments = sum(total_ask_comments) / len(ask_posts)
ValueError: invalid literal for int() with base 10: 'H'
Hello @gera333, welcome to the community.,
This error message seems to indicate that you are passing a string that’s not an integer to the int()
function. Therefore, I suppose the problem is not in the code you posted but in the code you used to get to the ask_posts
list. Can you post the code you used before the code you posted?
1 Like
I just deleted [1] from ask_posts.append(row[1]) and etc. and it helped
I hope if somebody will stuck there with the same issue it would help you.
So the final solution could be:
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)
And then:
total_ask_comments = 0
for row in ask_posts:
total_ask_comments += int(row[4])
avg_ask_comments = (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 = (total_show_comments) / len(show_posts)
print(avg_show_comments)
Result: 14.038417431192661
10.31669535283993
1 Like
As I thought the problem was in how you got the ask_posts
list.
Good job finding the solution!
1 Like