15. Median


#1



I can't figure out what's wrong with my code; when the number of elements in the sequence is even, the median rounds up to a whole number instead of a decimal like it should :frowning:

def median(sequence):
    sort = sorted(sequence)
    if len(sequence) % 2 == 0:
        return (( sort[len(sort)/2] + sort[(len(sort)/2)+1]) / 2.0)
    else:
        return sort[len(sort)/2]


Replace this line with your code.


#2

Try going downwards, and not upwards.


#3

Thanks so much! You're a lifesaver


#4

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