When I run the script, I am getting:
"Oops, try again. median() returned 0 instead of 1"
If I comment out the return after the IF and replace it with "Return 1" (troubleshooting step only), then the script moves on to finding the median of [4,5,5,4], and I get the error:
"Oops, try again. median([4, 5, 5, 4]) resulted in an error: 2 is not in list"
I have reviewed the logic deveral times, but cant seem to see where I'm going wrong.
def median(orig_list): sort_list = sorted(orig_list) #put the list in order x = len(sort_list) #the list is x terms long if x == 1: return sort_list.index(1) # if list has only 1 item, just return it elif x % 2 == 0: # its even, find the middle two terms and average them. return (sort_list.index(x/2) + sort_list.index(x/2 +1))/2.0 else: #it is odd, find the middle term and return it return sort_list.index(x/2 + 0.5)