Median Problems


Having problems running this, it says code doesn't run to end. I can run this exact code using Atom with no problems whatsoever.

Practice Makes Perfect Median

Invalid syntax error pointing to the else clause?

I don't know why this won't run

def median(x):
    y = sorted(x)
    l = len(y)

    if l % 2 == 0:
        z = (y[(l//2)] + y[(l//2)-1]) / 2

        n = l // 2
        z = y[n]

    return z


The code you posted has no syntax errors, most likely you changed it in some subtle way while copying it, you ran something else

Also note that Atom is only an editor, it does not run your code - Python does


Thanks ionatan, I just double-checked and the code is verbatim. I ran the code successfully from my terminal (using Atom as my editor). So . . .I don't know if it's a bug or what.


Until there's a byte-by-byte comparison or I'm able to reproduce it, I'm still inclined to suggest that it looks the same only because the difference hasn't been spotted

paste the code you posted back into the exercise - I expect it'll run
you can also try other code (like an empty file)

If something is up with the exercise's editor then perhaps it's sending different content, refreshing the page would likely fix that (not something I'm aware happens, but then again there's a lot of code involved so it's a possibility)



My code is similar to yours and works fine:

def median(numbers):
    x = sorted(numbers)
    y = len(x)
    if y % 2 == 0:
        result = (x[(y / 2)] + x[(y / 2) - 1]) / 2.0
        result = x[y / 2]
    return result

hope that helps.


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