Median


#1



When I try to run the code, this happens:

Oops, try again. median([1]) resulted in an error: 'NoneType' object has no attribute 'getitem'

Can anyone help me with why this error comes up, and why it comes up?


def median(listA):
    length = len(listA)
    result = 0
    listA = listA.sort()
        
    if length % 2 == 0:
        length = length / 2
        middle = listA[length + 1]
        middle_2 = listA[length]
        result = (middle + middle_2) / 2.0
      
    else:
        length = length / 2
        result = listA[length]
        
 
    return result


#2

wait, I solved it, no matter.

Solved it by using listB = sorted(ListA) and replacing wherever it mentions listA with listB. I also did middle = listB[length - 1] instead of middle = listA[length + 1]


#3

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