Why is my median incorrect?

I did it absolutely correct and yet it says my coding is incorrect and returns 4.0 when it actually returns 4.5 as intended.

def median(numbers):
middle = len(numbers) // 2
if len(numbers) % 2 == 0:
median = float((numbers[middle] + numbers[middle - 1]) / 2)
median = float(numbers[middle])
return median

print(median([102, 56, 34, 99, 89, 101, 10, 54]))

1 Like

when the division involves two integers ( 9 / 2 ) python will round down to the nearest integer, casting to float after the division means the damage is already done


Actually the division prior only matters for ODD numbers because if it’s even it divides into an integer anyway. Take your example, if you have a list that is 9 long, divided by two is 4.5, rounded down is 4, yet when it’s an odd amount of numbers the middle number is actually place 5 in the list but when I’m displaying the number I am using THE INDEX, which starts as 0 and that means the middle number is the FOURTH index, which translates to the FIFTH number in the list, since I return the 4th INDEX, which is the FIFTH number.

I’m using Pycharm, the code I posted works perfectly in PyCharm, so as I say, something is wrong with CodeAcadaemy in this instance.

that is not my example, i was doing the math of this line:

median = float((numbers[middle] + numbers[middle - 1]) / 2)

based of the list you where struggling with ([4, 5, 5, 4])

no, python made breaking changes moving forward, including in division behavior. so you are experiencing an additional obstacle because of these breaking changes.

Sorry but you’re wrong, check and see the exact same code for yourself functioning exactly as intended
4, 5, 5, 4 length is 4.
middle = len(numbers) // 2 # In this case it is 2.
if len(numbers) % 2 == 0: # In this case it is
median = float(numbers[middle] + numbers[middle = 1]) / 2)

In this instance I am storing as a float the equation of (5 + 4) / 2 which is 4.5, being stored as a float, in median and returning it, which is 4.5

in python3, you are right, the output is 4.5, but in python2 (which codecademy uses) the division behavior works different:

which i have attempted to explain


I’m not struggling in the slightest as you can clearly see unless you’re saying my PyCharm is magically broken and not functioning as intended, I type cast the equation as type float which will store the equation, as a float in to the variable called median.

i am not saying pycharm is broken, i just say that there are two major python versions which are not compatible. You can’t expect code from python2 to work flawlessly in python3 and vice versa


Thank you for taking the time and baring with me and i am very sorry if I came across in a negative way towards you at all, it’s my bad for not understanding properly and I should have thought better about my responds and what it is you’re explaining.

Keep up the excellent work you’re doing here and in future I’ll do better to articulate, respond and understand what I’m being told.


apology accepted, i understand, programming can be frustrating :slight_smile:

although that is true, the response was very much not where you where expecting, so then it can be difficult to accept

i will, thank you :slight_smile:

Given your response, i assume you solved the issue? :slight_smile:

PS, i also wished the codecademy course was python3, but i don’t have a say in these things. Furthermore, understanding some of the differences can make you better at python and maybe even programming in general.

i m actually having the same problem in python 2 plz help

please read this topic:

How to ask good questions (and get good answers)

with the information you currently provided, its really difficult to help you

1 Like

Thank you. This was killing me too. I had my code printing every step of the way and it was vexing me to no end.