Median, nearly complete I think


#1


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

Oops, try again. median([4, 5, 5, 4]) returned 5 instead of 4.5


I think the odd amount works, just having trouble calculating the median when the list is even. Thanks


def median(fun):
    fun.sort()
    middle = len(fun) / 2
    if fun[middle] == int(fun[middle]):
        return fun[middle]
    elif fun[middle] == float(fun[middle]):
        total = (fun[middle-1] + fun[middle+1]) / (2.0)
        return total


#2

If the list is of integers, this will always be true. Work with the length as an indicator.


#3

Sorted, thanks a lot.

  if len(fun) % 2 != 0:
        return fun[middle]
  else:

#4

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