+ 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 odpowiedzi
+ 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