Median error

#1

I am getting the error: Oops, try again. median([1]) returned [] instead of 1

``````def median(crazy):
result = []
order = sorted(crazy)
total = len(order)
middle = total/2
if total%2 != 0:
result == order[middle]
elif total == 0:
result == crazy[0]
else:
result == (order[middle]+order[middle+1])/2.0
print result
return result``````

#2

this line:

``elif total == 0:``

shouldn't you check if total is 1? Length is not zero index based (counting starts at 1)

#3

It must be affectation not equality.

#4

Got it to work:

def median(crazy):
result = []
order = sorted(crazy)
total = len(order)
middle = total/2
if total%2 != 0:
result = order[middle]
elif total == 0:
result == crazy
else:
result = (order[middle]+order[middle-1])/2.0
print result
return result

#5

Try this:

``````def median(crazy):
crazy = sorted(crazy)
total = len(crazy)
if total == 0: #if crazy = []...
print "Empty list"
elif total %2 == 1:
result = crazy[((total+1)/2)-1] #if crazy = ["a","b","c"] , result returns b
else:
result = crazy[(total/2)-1] #if crazy = ["a","b","c","d"] , result returns also b
return result``````

#7

how can that code possible work? But if you say you got it to work, it should be good