Median


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096


Oops, try again. median([1]) resulted in an error: list index out of range


def median(n):
    m = sorted(n)
    lm = len(m)
    even1 = m[lm / 2]
    even2 = m[lm /2 - 1]
    odd =  m[lm + 1 / 2]
    middle = 0
    if len(m) % 2 != 0:
        middle = odd
    else:
        middle = even1 + even2 / 2.0
        
    return middle

Can someone please explain what i am doing wrong?


#2

The line after your else statement.
Check your order of operations.


#3

Check order of operations throughout your code
Math doesn't necessarily read left to right.
(Pemdas)


#4

Your right forgot all about that but still getting the error

def median(n):
m = sorted(n)
lm = len(m)
even1 = m[lm / 2]
even2 = m[lm /2 - 1]
odd = m[lm + 1 / 2]
middle = 0
if len(m) % 2 != 0:
middle = odd
else:
middle = (even1 + even2) / 2.0

return middle

#5

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