Help me resolve this error


#1



15.15

Oops, try again. Your code did not run to end. Check the console window for errors!

Console:
File "python", line 8
elif len(x) % 2 == 0:
^
SyntaxError: invalid syntax

I think I'm on the right track, but I cannot resolve the above error. What is invalid about this syntax?


def median(x):
    x.sort()
    if len(x) == 1:
        return x[0]
    elif len(x) % 2 != 0:
        m = len(x)/2
    return m[x]
    elif len(x) % 2 == 0:
        m = len(x)/2
        n = (len(x)/2) - 1
    return (m[x] + n[x])/2.0


#2

if len(x) == 1:
        return x[0]
    elif len(x) % 2 != 0:
        m = len(x)/2
    return m[x] #HERE IS BAD INDENTATION
    elif len(x) % 2 == 0:

It is indented wrong, so that the if-statement stops, and you cannot add an elif.


#3

@atom.ion,

def median(x):
    x.sort()
    if len(x) == 1:
        return x[0]
    elif len(x) % 2 != 0:
        m = len(x)/2
    return m[x]   <===you want to get Element-m from list-x !!!!! 
                                 + indentation error !!
    elif len(x) % 2 == 0:
        m = len(x)/2
        n = (len(x)/2) - 1
    return (m[x] + n[x])/2.0  <===you want to get Element-m from list-x !!!! 
                                                  + indentation error !!

try it like

def median(x):
    x.sort()
    if len(x) == 1:
        return x[0]
    elif len(x) % 2 != 0:
        m = len(x)/2
        return x[m]
    elif len(x) % 2 == 0:
        m = len(x)/2
        n = (len(x)/2) - 1
        return (x[m] + x[n])/2.0
    
print median([0,3,4,5,7])
print median([0,3,4,5,7,11])