Median - code works in IDLE but not on here?


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5

<What do you expect to happen instead?>

My code should be returning 4.5 as it does on IDLE when using the same list


def median(numbers):
sort = sorted(numbers)
m = (len(sort)//2)
if len(sort)%2 == 0:
median = (sort[m-1]+ sort[m])/2
median = sort[int(m)]
return median

<do not remove the three backticks above>


changes where made to division behavior:

# python3
print 9 / 2 # 4.5
# python2
print 9 / 2 # 4

take a guess which python version your IDLE uses and which version codecademy uses? To make it work in python2 the division should involve a float to prevent python2 from rounding down to the nearest integer


Ah I see! Thanks so much for your help!