Get wrong result when calculate average comments from show posts- Guided Project: Exploring Hacker News Posts

Screen Link:

My Code:

#Calculate average comments from show posts:
total_show_comments = 0
for row in show_posts:
    num_show_comments=int(row[4])
    total_show_comments+=num_show_comments
    avg_show_comments=total_show_comments/len(show_posts)
    print(avg_show_comments)

What I expected to happen:

Since I use same code for calculating avg comments from ask posts I expected it will show the correct result
What actually happened:

0.0189328743545611
0.10671256454388985
0.10757314974182444
0.11015490533562823
0.11790017211703958
0.12048192771084337
....

Hi!
Your code calculates the average for each iteration and prints it out. Why do you think that could happen? Could it be due to incorrect indentation?

1 Like

Thank you so much, I got it now :slight_smile: