Median


#1


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


The code works, but the result is wrong. It say me: "Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5". But the result is 4.5. Sorry, but i can't copy code from codecademy. I try others web browsers but it doesn't work. Assumes that the indentation is Ok.


def median(x):
    sorted(x)
    result = 0
    longitud = len(x)
    if longitud % 2 == 0:
        middle_value = x[longitud/2]
        middle_value2 = x[(longitud/2)-1]
        result = (middle_value + middle_value2)/2.0
    else:
        result = x[longitud/2]
    return result
    
print median([4,4,5,5])

`


#2

if you call the function with a unsorted list:

print median([4,5,5,4])

it doesn't work, you don't store the result of the sort in a variable, store the result of sort:

x = sorted(x)

#3

OMG! What a stupid mistake. Thanks


#4