Hi guys im in course python -> practice makes perfect-> median... i have a problem with my code can u guys tell me what is worng with my logic?!?
def median(lst): if len(lst)%2==0: l=len(lst)/2 a=(lst[l]+ lst[l+1])/2 return a else: l=len(lst)/2 l += 1 l=lst[l] return l print median([1,1,2])
here is what i think should have happened! as the list is passed , in the if condition it is identified as not equal to 0 so switches to else part. There the i'm trying to finding the middle element here 3/2 =1 now l=1. in the next step
i'm adding +1 because of the list contains odd number of elements. Next i'm saving the value of lst in l and returning it.. this should have printed 1 as answer nut it gives 2 as answer and error message "Oops, try again. median() resulted in an error: list index out of range".. Thanks in advance guys.....