# Median

#1

Oops, try again. median([6, 8, 12, 2, 23]) returned 7.0 instead of 8

The code below return 8 when this function recive [6, 8, 12, 2, 23] as parameters instead of 7.0. I try in my shell and works fine, but its python 3.5 running in it. I would like to know if someone would know to tell me why this, or if my code is wrong. thanks in advance

``````Replace this line with your code.
def median(num_list):
num_list = sorted(num_list)
mid = len(num_list)/2
result = 0
if isinstance(mid, int):
result = (num_list[mid] + num_list[mid-1])/2.0
else:
result = num_list[int(mid)]
return result``````

#2

Not sure. Have faced similar issue with [4,5,5,4] in my code which is working in my laptop but not in the editor here. I am using python 3.4. However, this code is working in editor as well
def median(a):

``````l = sorted(a)
length = len(l)
mid = int(length/2)
if length%2==1:
return l[mid]
else:
return (l[mid]+l[mid-1])/2.0``````

Can't say what fixed it!

#3

Python 2.x is different from Python 3.x in some important respects.

In Python 2.x, which Codecademy still uses as of December 10, 2016, the `/` operator performs `int` division when both operands are `int`s, and performs `float` division when at least one of the operands is a `float`. In contrast, the `/` always performs `float` division in Python 3.x.

Codecademy may soon by offering Python 3.x.

In both Python 2.x and 3.x, the `//` operator always performs `int` division.

@ajaxninja88083, after the following statement is executed, `mid` will always be an `int` in Python 2.x, regardless of whether `num_list` contains an even or an odd number of items ...

``mid = len(num_list)/2``

Therefore, the condition here will always be `True` in Python 2.x ...

``if isinstance(mid, int):``

Use this condition instead ...

``if len(num_list) % 2 == 0:``

In the interest of clarity and portability, it would be best to change this ...

``mid = len(num_list)/2``

... to ...

``mid = len(num_list) // 2``

#4

Thanks for the explanation appylp, now i understand whats im doing wrong. Thanks for help

#5