def median(x):

sorted_x = sorted(x)

if len(sorted_x) % 2 == 0:

while len(sorted_x) > 2:

sorted_x.remove(sorted_x[0])

sorted_x.remove(sorted_x[-1])

return sum(sorted_x) / 2

else:

while len(sorted_x) > 1:

sorted_x.remove(sorted_x[0])

sorted_x.remove(sorted_x[-1])

return sorted_x[0]

When I put this into a text editor, it works. However, the same code gives me the following error in Codecademy: "median ([4, 5, 5, 4]) returned 4 instead of 4.5 "