Okay so I feel confident that this is as refined as I could make my code. I basically made 2 if statements: one that takes the middle number of a list whose length is an odd number; the other takes the middle two numbers and averages them together.
What am I doing wrong?
def median(lst): lst = sorted(lst) #Turns lst into a sorted list if len(lst) % 2 != 0: return lst[int((len(lst)/2.0))] #Returns the item that sits in the middle, assuming the length of the list is an odd number else: if len(lst) % 2 == 0: return ((lst[len(lst)]/2.0) + ((lst[len(lst)]/2.0)-1.0) /2.0) #Returns the average of the 2 items that sit in the middle, assuming the length of the list is an even number
Here's the error I am getting: > Oops, try again. median([4, 5, 5, 4]) resulted in an error: list index out of range