Median! Help!


#1


It says this "Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5"

Why the answer is not 4.5?


def median(numbers) :
    new=[]
    med=0.0
    index=0
    
    length=len(numbers)
    if length==1 :
        med=numbers[0]
    elif length%2!=0 :
        sorted(numbers)
        index=length%2
        med=numbers[index]
    elif length%2==0 :
        sorted(numbers)
        index=length%2
        med=numbers[index]+numbers[index-1]
        med=med/(2.0)
    return med


#2

here:

sorted(numbers)

you sort the list, but you don't store the sorted list anywhere/not in a variable?


#3

it still does not yield a correct answer


#4

can i see an updated version of your code?


#5

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