median([6, 8, 12, 2, 23]) returned 10.2 instead of 8

whit this list works:

def median(x):
    l = float(len(sorted(x)))
    total = 0
    new = 0
    for u in x:
        total = total+u
        new = total/ l
    return new
print median([6, 8, 12, 2, 23])


Your function looks to return an average, not a median. The median may be close to the average, but it is not the same kind of computed value.

A median is the middle value in a sorted sample if the length is odd; it is the average of the middle two values if the length is even.


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