Median


#1



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


Oops, try again. median([1]) resulted in an error: list indices must be integers, not float


It works properly in my PyCharm software however it gives error here.


import math

def median(n):
    x=sorted(n)
    v=math.ceil(len(x)/2)
    if len(x)%2==0:
        median_calculated=(x[v-1]+x[v])/2
    else:
        median_calculated=x[v-1]
    return median_calculated


#2


#3

changes where made to math.ceil(), the lessons run python2, you run python3

from stackoverflow:

As pointed out by other answers, in python they return floats probably because of historical reasons to prevent overflow problems. However, they return integers in python 3.

source


#4

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