Median: List is not callable!


#1



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



def median(num):
    num=sorted(num)
    l=len(num)
    if l%2!=0:
        return num(l/2)
    else:
        k=l/2
        p=(num(k)+num(k-1))/2
        return p

Where I am going wrong? can't figure it Out. Help will be highly appreciated :slight_smile:


#2

i will help in steps, please try to figure out further yourself first, if more help is required post an updated version of your code, the problem you currently have is in this line:

return num(l/2)

using () will cause a function call, but you want to access the list by index, for which we use square brackets:

return num[l/2]

#3

Thanks sir!
Finally after few changes it really worked. How silly error I made there!

The final code was like this:

def median(num):
num1=sorted(num)
l=len(num1)
if l%2!=0:
m=l/2
return num1[m]
else:
k=l/2
p=(num1[k]+num1[k-1])/2.0
return p