Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Data Science Basics Describing Data Calculate Averages

Greg Agamalian
Greg Agamalian
3,298 Points

Did not calculate the same average

I think my code matches but for some reason the averages that I have calculated are different. Here are the results that I generated

Average =  139.0775806770937
139.08
Average of 1st half = $79.26
Average of 2nd half = $59.78
from s2v3 import *

def find_average(data_sample, header = False):
    if header:
        data_sample = data_sample[1:]
    total = calculate_sum(data_sample)
    size = number_of_records(data_sample)
    average = total / size
    return average

average_price = find_average(data_from_csv, True)
print("Average = ", average_price)
print('{:03.2f}'.format(average_price))  # this is the format for two digits after the decimal

midpoint = round(number_of_ties / 2)
message = 'Average of {} half = ${:03.2f}'
print(message.format("1st", find_average(data_from_csv[:midpoint], True)))
print(message.format("2nd", find_average(data_from_csv[midpoint:], False)))

[MOD: fixed formatting -cf]

3 Answers

It looks like you program is working perfectly.

The reason the whole data set averages are different comes down to rounding. When you tell the program to display only to the hundredths place you are in effect telling it to round to the nearest hundredth.

The first half and second half averages are different because one half has larger values than the other.

If I've misunderstood just reply to this with more detail about your question and I'll try to give you an answer.

Brandon Wall
Brandon Wall
5,512 Points

I generate the same results as Gregory Agamalian , I was about to ask this very same question because my results are different from the ones the instructor gets in the video, but looking at yours I too got 79.26 and 59.78. The data file we are working with in workspaces must be different from the one the instructor is using.

I know this is from several years back, but I also had differing averages from the video. Has this issue ever been resolved? It's not just a matter of rounding because there are large differences in the numbers. In fact, ours is about half of what the instructor is showing.

How can we know if the data file for this class is different from the one the instructor is using?