Median help! (fixed, was a silly mistake)


#1



Hi everyone, I'm currently stuck on the median exercise and I'm not sure why my code isn't working.


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


I keep getting the error "Oops, try again. median([6, 8, 12, 2, 23]) returned 6 instead of 8" - I assume that its sorting properly so I must've done something wrong I can't find


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

Thanks in advance!


#2

Never mind - I only needed n/2 instead of n/2 - 1 because the division rounds down. Thanks anyway!


#3

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