Median


#1




It returns the error message: Oops, try again. median([1, 34, 1, 6, 8, 0]) returned 4.5 instead of 3.5

Why does it not work? I found that for num2, it return 8 instead of 6, but I don't know why.


def median(nums):
    sort = sorted(nums)
    length = len(sort)
    if length % 2 == 0:
        num1 = sort.pop((length/2)-1)
        num2 = sort.pop(length/2)
        avg = float(num1 + num2) / 2
    elif length % 2 == 1:
        med = sort.pop(length/2)
        return med


#2

pop might return the number from the list, but it also removes the items from the list. Maybe just retrieve the value without using pop?

python doc - lists, look at what pop does


#3

Got it, thanks so much!


#4