# Median

#1

``````def median(lst):
lst=lst.sort()
n=len(lst)
if n==1:
m=n
elif (n%2==0):
m=lst(n/2)+lst(n/2+1)
else:
m=lst(n/2)
return m
"""what is the wrong with this code"""``````

``Replace this line with your code.``

#2

here:

``lst=lst.sort()``

`sort()` will manipulate the list directly, so there is no need to store the result of `.sort()` action in a variable, in fact, this causes the problem because `.sort()` returns None:

``````x = [4,5,5,4].sort()
print x # output None``````

#3

def median(lst):
lst.sort()
n=len(lst)
if n==1:
m=n
elif (n%2==0):
m=lst(n/2)+lst(n/2+1)
else:
m=lst(n/2)
return m
#
Oops, try again. median([4, 5, 5, 4]) resulted in an error: 'list' object is not callable

#4

here:

``m=lst(n/2)+lst(n/2+1)``

to access list by indexes we use square brackets, using parentheses make it function calls

#5

``````def median(lst):
lst.sort()
n=len(lst)
if n==1:
m=lst[0]
elif (n%2==0):
m=(lst[n/2]+lst[n/2+1])/2
else:
m=lst[n/2]
return m``````

"""plz help me is there any mistake in formulea"""

#6

there are two problem, for `[4,5,5,4]`, what are two middle indexes? And which indexes are you using?

the median of [4,5,5,4] is 4.5 (9/2) however, if the division involves two integers python will round down (floor), so you will need to use a float somehow in your division

#7

@javacoder32998 don't go and hijack another topic, i am asking you a question, you can answer it, then i can help you further

#8

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