Median Practice


Any ideas as to why this isn't working? I am fairly certain the median is the middle number which i am pretty sure my program returns. Below is my code and the error message I keep getting.

def median(lst):
        if len(lst) % 2 != 0:
             return lst[len(lst)/2]
            b = lst[len(lst) / 2 + 1]
            a = lst[len(lst) / 2]
            c = (a + b) * .5
            return c

Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8


Try subtracting 1 instead of adding.


It works now. Thanks!


A clever way to create a float solution, implicitly. Now wilh Python 3.x making this de facto it doesn't mean that much. But in terms of 2.x I believe it does.

c = float(a + b) / 2

is as explicit as it gets. I favor this approach to the point of defending it. Don't let implicitness skew your perspective on coding. Get hooked and get railroaded.


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