15.median


#1




I have this error message
Oops, try again. median([1]) resulted in an error: list index out of range


I think my code is must work correctly. Where is the problem?


Replace this line with your code. 
def median(n):
    n=sorted(n)
    total = len(n)
    if total % 2 != 0:
        mid = (total+1)/2
        med = n[mid]
        
    else:
        mid_1=(total+2)/2
        mid_2=total/2
        med=(mid_1+mid_2)/2
    return med


#2

look:

1 / 2 = 0
3 / 2 = 1
5 / 2 = 2
7 / 2 = 3
9 / 2 = 4

dividing the length by two perfectly gives the middle index, so i don't see why you do +1


#3

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