Median


#1



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


My code returns:
Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5


Replace this line with your code. 
def median(numbers):
    n = sorted(numbers)
    x = 0
    y = 0
    z = len(n)
    if z == 1:
        return z
    elif z == 0:
        return 0
    elif z%2 == 1:
        return n[(z+1)/2]
    elif z%2 == 0:
        x = n[z/2]
        y = n[(z +2)/2]
        return ((x+y)/2.0)

Please help - thank you!


#2

here:

        x = n[z/2]
        y = n[(z +2)/2]
        return ((x+y)/2.0)

z/2 give the right middle , so i am not sure (z+2)/2 give the left middle index

for a list with length 4, the indexes are: 0, 1, 2 and 3. So the middle indexes are 1 and 2


#3

Thank you! I forget that indices started at 0


#4