+ 2

Please, explain what is the error in my python code to return the median of a list with integers?

if list has even no of elements,it should return the average of 2 middle numbers. def median(A): Ā  A=A.sort() Ā  l=len(A) Ā  c=int(l/2) Ā  if(l%2!=0): Ā  Ā  return(A[c]) Ā  else: Ā  Ā  return((A[c]+A[c-1])/2) print(median([1,3,7,9,2])) print(median([2,4,5,1]))

5th Nov 2017, 6:16 PM
Abhinav
Abhinav - avatar
5 Answers
+ 3
If statements don't have parentheses. The line should be: if l%2!=0: # statements
5th Nov 2017, 6:18 PM
LunarCoffee
LunarCoffee - avatar
+ 3
Oh. (im embarrassed because i've already made 2 mistakes today)
5th Nov 2017, 8:31 PM
LunarCoffee
LunarCoffee - avatar
+ 1
@ace thanks sir,I got it now,šŸ™
5th Nov 2017, 7:30 PM
Abhinav
Abhinav - avatar