I'm Having Trouble With Median


Stuck at meduan. I'd appreciate any help

median([1]) resulted in an error: unsupported operand type(s) for /: 'list' and 'float'

Replace this line with your code. 

def median(listt):
    if len(listt) % 2 == 0:
        first_index = len(listt) / 2 
        second_index = (first_index - 1)
        answer = float(listt[first_index]) / float(listt[second_index])
        answer = listt[len(listt / 2)]
    return answer


Needs to be assigned as this is not an in-place method.

This will not give a median of an even length list. Your are dividing the two neighbouring elements, one by the other. It should be the sum of the two divided by 2.


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