我不太确定自己的代码在做错什么以获取此错误。我一直在环顾四周,看看是否可以找到有人遇到同样的问题,但到目前为止我还没有成功。代码是:
def sort(dislis):
for index in range(0,len(lst)):
currval= dislis[index]
position = index
while position>0 and dislis[position-1]>currval:
dislis[position] = dislis[position-1]
position = position-1
dislis[position]=currval
回溯错误是:
Traceback (most recent call last):
File "C:", line 49, in <module>
distance()
File "", line 47, in distance
sort(dislis)
File "", line 20, in sort
currval= dislis[index]
IndexError: list index out of range
答案 0 :(得分:3)
def sort(dislis):
for index in range(0,len(dislis)):
currval= dislis[index]
position = index
while position>0 and dislis[position-1]>currval:
dislis[position] = dislis[position-1]
position = position-1
dislis[position]=currval
return dislis
result = sort([3,4,2,1])
print(result)
那里有一些阻止它运行的小错误,请尝试一下。
此外,我强烈建议仅使用空格,而不使用制表符。