# Median?

#1

Oops, try again. median([1]) resulted in an error: list index out of range

So I sorted the code and then I found the length.
When the length is divided by 2 and the remainder is 1 then it divides the index by 2 and returns the number in that index (i think).
Then if the remainder is equal than 0 then it divides the index of the number and gets that number. Then it takes divides the index again by 2 and then subtract by 1 to get the previous index
Then it takes those two numbers and divides it to get the median

``````def median(list_input):
list_input= sorted(list_input)
length=len(list_input)
if  length%2==1:
a = list_input[length]/2
return a
elif length%2==0:
a = list_input[length]/2
b = list_input[length]/2-1
c = a/b
return c``````

#2

This will not be a median. We need to add the middle two values and take their average when list length is even.

Aside

Can you see the repeated code patterns? See if you cannot find a way to simplify that so your code is more easily readable.

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.