2015-03-13 28 views
0

我需要从列表中删除一个单词,然后显示单词 - 我可以很好地做到这一点。 但是当我删除第二个单词并附加之前删除的单词时,找不到列表。删除带有'pop'的单词

replword = words.pop(9) 
except IOError: 
    print("WARNING: The text file cannot be found. The program will not run without error.") 

'while menu = true' 
    try: 
     threeByThree() #function: threeByThree 
     #suffle words 
     #remword = words[9] 
     #words = random.shuffle(words) 
     print(remword) 
     newword = replword 
     words.append(newword) 
     threeByThree() 
     #firstNineWords = words[:9] 
     #lastWord = words[-1] 
     words[random.randrange(9)] = lastWord #generate integer between 0 and 9 
     print("") 
     threeByThree 
    except NameError: 
     print("WARNING: Because the file is not found, the list 'words' is not defined.") 

我已经尝试了代码在多个不同的订单,我仍然得到名称错误说列表中没有定义,当它被定义时,它拉词语。

我需要能够给两个单词分配2个变量,一个被删除并添加,然后第二个删除 - 这个问题后,我可以自己做。

+1

你明白,通过'try/except NameError'保护13行代码(其中5个现在已被注释)意味着你拼错整个部分名称的任何对象都将导致你的程序打印出“WARNING :因为文件没有找到......“等等,这不会是真的吗? – 2015-03-13 17:57:33

+0

什么是“threeByThree”,为什么最后一行不是'threeByThree()'? – Jkdc 2015-03-13 17:57:33

+2

此代码无法运行,请提供*完整运行示例*。有一件事我可以指出:'threeByThree'是一个函数,但在最后一次打印之后,它被称为内建函数。 – alfasin 2015-03-13 17:57:38

回答

0

欢迎来到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 
>>> 

正如我过去告诉过的学生:一个程序完全按照被告知要做的事情,而不是你想做的事情。