Guided Project : Step 4 : Getting different average than solution book

Screen Link:

https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/4/calculating-the-average-number-of-comments-for-ask-hn-and-show-hn-posts
My Code:

total_ask_comments= 0 

for posts in ask_posts:
    total_ask_comments += int(post[4])

avg_ask_comments = total_ask_comments / len(ask_posts)
print ('Average Comments on Ask Post :', avg_ask_comments)


#SHOW POSTS
total_show_comments= 0 

for posts in show_posts:
    total_show_comments += int(post[4])

print (total_show_comments)
avg_show_comments = total_show_comments / len(show_posts)
print ('Average Comments on Show Post :', avg_show_comments)

What I expected to happen:

I am getting the average to be 58 for both comments for both show and ask. However the solution books answer doesn’t match. My length of all the list is matching with the solution set. Any pointers as to what I am doing wrong.

hey @tyche2k

you have used posts as a counter in the for loop, however you are using post while adding total comments. why is that?

I tried with int(post[4]) updated to int(posts[4]) and your code works. gave me results - 14 for ask and 10 for show.

hi @Rucha

Thank you for pointing that out. Guess I overlooked it.

Regards

Tanveer

1 Like