object of type NoneType has no len

后端 未结 2 1868
夕颜
夕颜 2021-01-16 01:22
def medianeven (L):
    while len(L) > 2:
        L = L[1:(len(L)-1)]
    return average (L)

def medianodd (L):
    while len(L) > 1:
        L = L[1:(len(L)-         


        
2条回答
  •  感情败类
    2021-01-16 02:02

    .sort() is in-place and returns None.

    Change this line:

    new = L.sort()
    

    To just this:

    L.sort()
    

    And replace all of your instances of new with just L. You also need to return the results of those function calls:

    if a % 2 == 0:
        return medianeven(new)
    else:
        return medianodd(new)
    

    Also, Python's slices support negative indices, so this code:

    L[1:(len(L)-1)]
    

    Can be simplified to just

    L[1:-1]
    

提交回复
热议问题