[英]object of type NoneType has no 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)
It says TypeError: object of type 'NoneType' has no len()
. 它说
TypeError: object of type 'NoneType' has no len()
。 Both medianeven
and medianodd
work, but median
itself is not functioning. median
medianeven
和median
medianodd
起作用,但是median
本身不起作用。
.sort()
is in-place and returns None
. .sort()
就位并返回None
。
Change this line: 更改此行:
new = L.sort()
To just this: 为此:
L.sort()
And replace all of your instances of new
with just L
. 并仅用
L
替换所有new
实例。 You also need to return
the results of those function calls: 您还需要
return
这些函数调用的结果:
if a % 2 == 0:
return medianeven(new)
else:
return medianodd(new)
Also, Python's slices support negative indices, so this code: 另外,Python的切片支持负索引,因此此代码:
L[1:(len(L)-1)]
Can be simplified to just 可以简化为
L[1:-1]
The sort() method call you have in the line: 您在该行中进行的sort()方法调用:
new = L.sort();
doesn't return anything. 不返回任何东西。 Thus 'new' holds 'None', which has no length.
因此,“新”持有“无”,没有长度。 I believe doing L.sort() will sort the list in place.
我相信这样做L.sort()可以对列表进行排序。 Don't need to store it in 'new'.
不需要将其存储在“ new”中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.