hi im on this lesson and ive done my coding here it is
def median(lit): lit.sort() # sort the list if len(lit) % 2 != 0: # if there are an odd number of items in list middle = len(lit) / 2 # find the middle index, since index starts at 0 i divide return lit[middle] # access the value within that index to get the median if len(lit) % 2 == 0: # if there are an even number of items in list mid1 = (len(lit) - 1) / 2 # find the lower middle index, since index starts at 0 i minus 1 and divide mid2 = (len(lit) + 2) / 2 # find the upper middle index, i add 1 and divide return (lit[mid1] + lit[mid2]) / 2 # access the values of those two indexes, add them and divide by 2 to get the median
the error i get is
median([4, 5, 5, 4]) resulted in an error: local variable ‘middle’ referenced before assignment
i wanted to know how to fix this error or if what im doing is not even close to the right thing.
one more question, to could u find a half index, such as the 2.5th piece instead of finding the value of the 2nd and 3rd and finding the mean of those?