First of all, when I run the solution given by codecademy, there’s a error “TypeError: list indices must be integers, not float”.
Secondly, below is my code, and it says invalid syntax in line 10
return result. What could possibly go wrong with this? Besides, is the
int() necessary since we already know the number inside a[ ] must be an integer? Finally, codecademy tells me that this code turns out to be 4 instead of the correct number 4.5. I have no clue which part is wrong. Please help! Thanks a lot.
def median(a): a = sorted(a) amount = 0 for item in a: amount = amount + 1 if amount % 2 != 0: result = a[int((amount+1)/2-1)] else: result = (a[int((amount/2)-1)]+a[int(amount/2)]/2 return result a = [4,5,5,4] print(median(a))