# Median - wrong error?

#1

Hi everyone! After a lot of trying and testing I got a working code to take the median of a list, regardless if it is an even or oneven number. However it still doesn't run in Codeacademy as it runs on Atom.

Does anybody know what happens there? Any tips are welcome of course! Thanks!

It gives the error: Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5. But I tested it in Atom and it does give the 4.5 as needed.

``````def median(x):
lijst = sorted(x)
if len(x) % 2 == 1:
return lijst[int((len(x)/2))]
else:
return (lijst[(int((len(lijst)-1)/2))]+lijst[(int(len(lijst)/2))])/2``````

#2

Change that very last 2 to 2.0
That will allow for floats.

#3

Thanks it worked! So close.........

#4

But strangely enough it (changing the last 2 to 2.0) wasn't necessary in Atom

#5

It's probably because you have Python 3/Atom is using Python 3. Then the division behaviour allows for float division by just doing something like `5/2` -> `2.5` in Python 3 but `2` in Python 2.

#6

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