欢迎来到bug狩猎101。您需要退后一步,尝试澄清您遇到的问题。您提供的代码是某些较大脚本的不完整部分。正如上面的评论,你引用threeByThree作为一个函数和内置。此外,假设单词是全局的并且始终可用,那么您不会将任何事情传递给该函数。你可以做的最好的事情是打开一个REPL控制台并解决问题。
>>> listOfWords = ["My","dog","has","fleas","and","ticks","and","worms","he","al
>>> listOfWords
['My', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'worms', 'he', 'also', 'smells']
>>> removeditem = listOfWords[4]
>>> removeditem
'and'
>>> listOfWords
['My', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'worms', 'he', 'also', 'smells']
>>> removeditem = listOfWords.pop(4)
>>> listOfWords
['My', 'dog', 'has', 'fleas', 'ticks', 'and', 'worms', 'he', 'also', 'smells']
>>> removeditem
'and'
>>> anotherremoveditem = listOfWords.pop(1)
>>> listOfWords
['My', 'has', 'fleas', 'ticks', 'and', 'worms', 'he', 'also', 'smells']
>>> anotherremoveditem
'dog'
>>> listOfWords.append(removeditem)
>>> listOfWords
['My', 'has', 'fleas', 'ticks', 'and', 'worms', 'he', 'also', 'smells', 'and']
>>> listOfWords.append(anotherremoveditem)
>>> listOfWords
['My', 'has', 'fleas', 'ticks', 'and', 'worms', 'he', 'also', 'smells',
'and', 'dog']
>>>>>> def threeByThree(words):
... print(words[:3])
... print(words[3:6])
... print(words[6:9])
...
>>> threeByThree(listOfWords)
['My', 'has', 'fleas']
['ticks', 'and', 'worms']
['he', 'also', 'smells']
>>> threeByThree(listOfWords[9:])
['and', 'dog']
[]
[]
>>>
当然,如果你输入了错误的东西,你可以看到错误消息
>>> threeByThree(sistOfWords[9:])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sistOfWords' is not defined
所以,如果你到threeByThree()调用引用变量话你没有通过,以及Try/Except循环捕捉异常,你会得到错误的错误信息。
>>> def three():
... print(words[0:3])
...
>>> three()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in three
NameError: global name 'words' is not defined
>>>
正如我过去告诉过的学生:一个程序完全按照被告知要做的事情,而不是你想做的事情。
你明白,通过'try/except NameError'保护13行代码(其中5个现在已被注释)意味着你拼错整个部分名称的任何对象都将导致你的程序打印出“WARNING :因为文件没有找到......“等等,这不会是真的吗? – 2015-03-13 17:57:33
什么是“threeByThree”,为什么最后一行不是'threeByThree()'? – Jkdc 2015-03-13 17:57:33
此代码无法运行,请提供*完整运行示例*。有一件事我可以指出:'threeByThree'是一个函数,但在最后一次打印之后,它被称为内建函数。 – alfasin 2015-03-13 17:57:38