NoneType 类型的对象没有 len [英] object of type NoneType has no len
本文介绍了NoneType 类型的对象没有 len的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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)-1)]
return L[0]
def median (L):
new = L.sort()
a = len(new)
if a % 2 == 0:
medianeven(new)
else:
medianodd(new)
它说 TypeError: object of type 'NoneType' has no len()
.medianeven
和 medianodd
都有效,但 median
本身不起作用.
It says TypeError: object of type 'NoneType' has no len()
. Both medianeven
and medianodd
work, but median
itself is not functioning.
推荐答案
.sort()
就地并返回 None
.
更改这一行:
new = L.sort()
就这样:
L.sort()
并用 L
替换所有 new
实例.您还需要返回
这些函数调用的结果:
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)
另外,Python 的切片支持负索引,所以这段代码:
Also, Python's slices support negative indices, so this code:
L[1:(len(L)-1)]
可以简化为只是
L[1:-1]
这篇关于NoneType 类型的对象没有 len的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文