如果您能帮助调试此代码,我将不胜感激:
testing = """There is something unique about this line
in that it can span across several lines, which is unique and
useful in python."""
listofthings = []
i = 0
while i < len(testing):
if testing[i] == " ":
listofthings.append(i + 1)
i = i + 1
listofthings.insert(0, 0)
listofthings.append(len(testing))
print listofthings
word_list = []
i = 0
while i < len(listofthings):
l = i + 1
x = listofthings[i]
y = listofthings[l]
word = testing[x:y]
word_list.append(word)
i = l
print word_list
我不知道为什么会出现index out of range
错误。我明白错误的含义,但不确定我做错了什么。奇怪的是,只有当我运行上面的代码时才会发生这种情况。运行此程序时不会出现任何错误:
我对Python相当陌生(将持续三天),所以我确信这是一个被忽略的愚蠢语法错误。。。在
当
^{pr2}$i
对应于最后一个元素时您正试图访问最后一个元素旁边的元素。这就是它抛出错误的原因。在
当
i=length-1
时,则y=length
,这是一个错误。Python数组索引从0开始,因此最大地址是length-1
列表
listofshit
的长度为21,索引范围从0到20。在最后一个循环中,i
是20,l
是21,所以有一个超出范围的错误。我想下面的代码就是你想要的:相关问题 更多 >
编程相关推荐