In Practice Makes Perfect: 15. Median, I came up with this code.
Python tells me: "median([4, 5, 5, 4]) returned 5.0 instead of 4.5". Which I don't understand since I've already sorted the list. Where did I go wrong?
def median(numbers): snumbers = sorted(numbers) if len(snumbers) % 2 == 0: index1 = len(snumbers) / 2 index2 = index1 + 1 average = (snumbers[index1] + snumbers[index2]) / 2.0 return average else: index3 = len(snumbers)/2 average = snumbers[index3] return average