"Oops, try again. median([4,5,5,4]) resulted in an error: list index out of range."
This is the first exercise in the practice section that I've been able to get so far in w/o consulting the Q&A, which is fun. But now I'm really stuck. I used print to test throughout, and I know it's getting as far as the the if/elif/else statements, but then I get hung up on why it fails on getting the average.
# define numlist def median(numlist): # sort the sequence numerically, using sorted() sortedlist = sorted(numlist) # count the number of values num = len(sortedlist) if num == 1: average = 1.0 return average # check if num is even... elif num % 2 == 0: # locate the middle two values middle_a = num / 2 # for a list [0,1,1,2,3,4] has num 6, so 6/3 = 2, so middle_a would be 2. middleindex_a = sortedlist[middle_a] middle_b = middleindex_a + 1 middleindex_b = sortedlist[middle_b] average = ((middleindex_a + middleindex_b) / 2.0) return average # if num is odd... else: # locate the middle value middle = num / 2 middleindex = sortedlist[middle] average = (middleindex / 2.0) return average return average print median(numlist)