【发布时间】:2021-03-23 09:40:43
【问题描述】:
def bubble_sort(alist):
size = len(alist)
for i in range(size):
index = alist[i]
for j in range(size - 1, i, -1):
if alist[j] < alist[j-1]:
temp = alist[j-1]
alist[j-1] = alist[j]
alist[j] = temp
def bsort(L):
if len(L) <= 1:
return L
else:
T = bubble_sort(L)
if L[0] <= T[0]:
return L[0] + T
else:
P = L[0] + T[-1]
Q = bubble_sort(P)
return T[0] + Q
print(bsort([5,4,3,9,1]))
收到此错误
if L[0] <= T[0]:
TypeError: 'NoneType' object is not subscriptable
【问题讨论】:
-
你没有从函数
bubble_sort返回任何东西。 -
所以,你现在已经知道答案了,但是下次调试的策略是:
NoneType is not subscriptable在该行代码上意味着L是无或T是无。没有其他东西可以导致该错误。所以,在if测试之前打印出这两个值,看看哪个是None,然后通过你的代码向后工作,看看为什么它没有正确设置。
标签: python recursion bubble-sort