Median


#1


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

It is saying "Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5"

I am returning 4.5 but its saying it is returning 5


def median(numbers):
    half = len(numbers)/2
    if len(numbers)%2 == 0:
        i = numbers[:half]
        j = numbers[half:]
        total = (i[-1] + j[0])/2.0
        return total
    else:
        c = numbers[:half]
        b = numbers[half:]
        return b[0]
        
numbers = sorted([4,5,5,4])

print median(numbers)

This is working but it is not passing. Can someone explain why?


#2

Remember that a median is taken from the middle of a sorted list.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.