+ 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]))
5 Answers
+ 3
If statements don't have parentheses. The line should be:
if l%2!=0:
# statements
+ 3
Oh. (im embarrassed because i've already made 2 mistakes today)
+ 1
@ace thanks sir,I got it now,š