Print function printing all lines of calculation of variable holding an average

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 row in ask_posts:
    num_comments = int(row[4])
    total_ask_comments += num_comments
    avg_ask_comments = total_ask_comments / len(ask_posts)
    print(avg_ask_comments)

What I expected to happen: So this is the code that I’ve written to find out the average of ‘ask comments’ and I have no doubts in the code itself. My doubt is that when I print the variable ‘avg_ask_comments’ it prints the following-

0.0034403669724770644
------
13.888761467889909
13.88933486238532
13.89105504587156
13.895642201834862
13.904243119266056
14.01834862385321
14.018922018348624
14.021788990825687
14.0223623853211
14.03211009174312
14.033830275229358
14.034977064220184
14.037270642201834
14.038417431192661

The last line is the correct answer and it should only be printing that, and not all the lines of calculation, like in the answer key. I dont know what I’m missing out on here. Thanks for your help :slight_smile:

Your print statement is inside the loop, so it prints the result of every iteration. To print the result of all iterations write your print statement outside the loop

1 Like

Thank youu for the answer, I should’ve paid more attention to what I was doing :sweat_smile: