python3'Nontype object'不可订阅

时间:2016-04-11 05:06:53

标签: list python-3.x return nonetype

我该如何解决这个问题?

def median(lst):
    lst_s=lst.sort()
    k=len(lst)

    if k%2==0:
        return ((lst_s[k/2]+lst_s[k/2-1])/2)
    elif k%2==1:
        return (lst_s[k/2-1/2])

res = median([1,2,3,7,6,5])
print('median:',res)

当我运行它时,它说

Traceback (most recent call last):
  File "E:\lab5_p4_2015123176.py", line 13, in <module>
    res = median([1,2,3,7,6,5])
  File "E:\lab5_p4_2015123176.py", line 7, in median
    return ((lst_s[k/2]+lst_s[k/2-1])/2)
TypeError: 'NoneType' object is not subscriptable

0 个答案:

没有答案