Median Help


#1



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


This is what I get when I run the code: Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


When I run this code through on python using the list [4, 5, 5, 4] it gives me 4.5, which is the correct answer, but I can't figure out why python is giving me the right answer, but not this program


def median(numbers):
    length = len(numbers)
    x = sorted(numbers)
    if len(x) % 2 == 0: 
        middle = (x[int(length /2)-1]) + (x[int(length/2)])
        median = middle / 2
        return (median)
    elif len(x) % 2 != 0:
        middle = (x[int(length/2)])
        median = middle
        return (median)


#2

In Python 2.0

/ - Division
If you divide an integer by integer you will get Integer.
To get float value you need to make one of the number to float.

hint**
2. 0?


#3

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