Median works in P2.7 and P3.6 but not Codecademy, can't see it


#1



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


No error messages from Python. Codecademy says that [4,5,5,4] returns 4 and should be 4.5.
Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


Ran this in Python 2.x and Python 3.x on two Apple machines and one Linux and they all returned 4.5. Tried many different combinations of numbers, both in variety and length and always got predicted results. So for now, I think the Codecademy has a bug, please prove me wrong.


def median(numbers):
    lnum = 0
    med = 0
    numbs=list(numbers)
    numbs.sort()
    lnum = len(numbs)
    if lnum/2 == lnum//2:
        med = (lnum//2)-1
        med = (numbs[med]+numbs[med+1])/2
    else:
        med = lnum/2
        med = numbs[med]
    return med

print (median([4,5,4,5]))


Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 
=============== RESTART: /Users/Billweigle/Documents/median.py ===============
4.5
>>>


#2

changes where made:

#python2 
9 / 2 = 4
# python 3
9 / 2 = 4.5

to how division is done in python. To achieve your result in python2, you will need a float


#3

My P2 testing must have been with a data set that "worked", most of my testing is done in P3. Thanks, that was driving me crazy.


#4

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