Python 删除带有'的单词;流行音乐';

Python 删除带有'的单词;流行音乐';,python,Python,我需要从列表中删除一个单词,然后显示这些单词-我可以做得很好。 但是当我删除第二个单词并附加之前删除的单词时,它找不到列表 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() #f

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

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.")
我已经尝试了多个不同顺序的代码,但仍然出现名称错误,表示列表未定义,当它在拉入单词时定义了列表


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

欢迎来到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
>>三乘三(SistofWord[9:]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“SistofWord”
因此,如果对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
>>>
>>定义三个()
...   打印(字[0:3])
...
>>>三
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第2行,共三个
NameError:未定义全局名称“words”
>>>

正如我在过去告诉学生的那样:一个程序完全按照命令去做,而不是你想让它做的。

你确实意识到,如果有13行代码(其中5行现在有注释)受到
try/except NameError
的保护,那么你在整个部分拼写错误的任何对象都会导致你的程序打印出来“警告:因为找不到文件…”等等。哪一项不正确?什么是
threeByThree
,为什么最后一行不是
threeByThree()
?此代码无法运行,请提供完整的运行示例。但我可以指出一点:
threeByThree
是一个函数,但在最后一次打印之后,它被调用时就好像它是一个内置的一样。threeByThree是一个包含print(单词[0:3])print(单词[3:6])print(单词[6:9])的函数