Median


#1




This is my error : Oops, try again. median([1]) resulted in an error: unsupported operand type(s) for %: 'list' and 'int'


Can someone please help me with this? I don't have any idea on what to change


def median(x):
    newlist = []
    if range(1, len(x))%2!=0:
        listlength = range(1, len(x)+1)
        num = listlength / 2
        middlenum = x[num]
    else:
        listlength = range(1, len(x)) 
        num = listlength / 2
        num2 = num + 1
        middlenum = num2 + num / 2
    return middlenum


#2

def median(x):
    if len(x)%2!=0:
        x.sort()
        listlength = len(x)+1
        num = listlength / 2
        middlenum = x[num]
    else:
        x.sort()
        listlength = len(x)
        num = listlength / 2
        num2 = num + 1
        middlenum = x[num2 + num / 2]
    return middlenum

I fixed some things but now I get .. Oops, try again. median([1]) resulted in an error: list index out of range
PLEASE HELP


#3

here:

listlength = len(x)+1

why length of list + 1?


#4

YEEEEEEEEES FUKIN DID IT

def median(x):
if len(x)%2!=0:
x.sort()
listlength = len(x)
num = listlength / 2
middlenum = x[num]
else:
x.sort()
listlength = len(x)
num = listlength / 2
num2 = num + 1
realnum = (x[num2-1] + x[num-1]) / 2.0
middlenum = realnum
return middlenum


#5