Median


#1

def median(listt):
listt.sort()
a=len(listt)
b=0
if a%2==0:
b=listt(b/2)+listt((b/2)-1)
b=b/2.0
else:
b=listt((b-1)/2)
return b

Error: list object not callable for median[1]


#2

To get the item in a list, the syntax is list[index] not list(index). You have round brackets but it should be squared ones.

Also, your variable a holds the length of the list while b is 0. Actually, you don't really need to define b=0 as you are defining b again later one as b=listt[b/2]+listt[(b/2)-1].

But you will get another error saying that your median is outputting the wrong number because you are thinking that b holds the length of the list but it's actually a (and odd # of items in list's median should just be the middle index (a/2)).


#3

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