Median


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096


Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


I cannot figure out why my code is not calculating the average correctly.


def median(x):
    average = 0
    middle = len(x) / 2
    y = sorted(x)
    if len(x) % 2 == 0:
        average = y[middle - 1] + y[middle]
        average = average / 2
        return average
    else:
        return y[middle]


#2

here:

average = average / 2

because the division involve two integers, python will round down (floor) the number. To solve this problem, use a float in one way or the other, i will leave that for you to figure out


#3

That worked, thank you!


#4