简体   繁体   English

Python:从列表中选择随机单词,然后将其删除

[英]Python: Choose random word from list then remove it

The question is to chooses a random word from a list of words you have defined, and then remove the word from the list. 问题是要从已定义的单词列表中选择一个随机单词,然后从列表中删除该单词。 The computer should display a jumble of the word and ask the user to guess what the word is. 计算机应显示单词的混杂物,并要求用户猜测单词是什么。 Once the player has guessed the word, another random word should be selected from the list and the game continues until the list of words is empty. 玩家猜出单词后,应从列表中选择另一个随机单词,然后游戏将继续进行直到单词列表为空。

When I run it, i have an error. 当我运行它时,我有一个错误。

Traceback (most recent call last):
   File "F:\Computer Science\Unit 3\3.6\3.6 #5.py", line 21, in <module>
     word_jamble (random_word)
   File "F:\Computer Science\Unit 3\3.6\3.6 #5.py", line 14, in word_jamble
    word = list(word)
TypeError: 'list' object is not callable

This is my program 这是我的程序

list = ['mutable', 'substring', 'list', 'array', 'sequence']

from random import shuffle

def word_jamble(word):
   word = list(word)
   shuffle(word)
   print ''.join(word)

from random import choice

random_word = choice(list)
word_jamble (random_word)
user_input = raw_input("What's the word? ")
if user_input == choice(list):
   del list[index(choice(list))]

You should change your first variable, list , to something else. 您应该将第一个变量list更改为其他内容。 It is being confused with the built-in list type, and your list object is of course not callable. 它与内置列表类型混淆了,您的list对象当然是不可调用的。

The main problem is the variable's name, list . 主要问题是变量的名称list Its a builtin type constructor's name. 它是内置类型构造函数的名称。 When you use say list , it shadows the builtin type's name. 当您使用say list ,它会掩盖内置类型的名称。 Apart from that, you can use pop method, like this, to get the random words out of the list easily 除此之外,您可以像这样使用pop方法将随机单词轻松从列表中删除

words_list = ['mutable', 'substring', 'list', 'array', 'sequence']
import random
while words_list:
    print words_list.pop(random.randrange(len(words_list)))

It means exactly what it says: 它的意思恰恰是它所说的:

TypeError: 'list' object is not callable

It is complaining about 它在抱怨

word = list(word)

because at this point, 因为在这一点上

list = ['mutable', 'substring', 'list', 'array', 'sequence']

has already happened. 已经发生了。

Once you make list a name for that particular list, it can no longer be a name for the built-in list class. list命名为该特定列表的名称后,该名称将不再是内置list类的名称。 A name only names one thing at a time. 名称一次只能命名一件事。

There are a few basic problems with the code: 该代码有一些基本问题:

list = ['mutable', 'substring', 'list', 'array', 'sequence']

list is the list constructor. list是列表构造函数。 You should never name your variables after python keywords. 永远不要在python关键字之后命名变量。


del list[index(choice(l))]

del is very rarely needed in python. 在python中很少需要del My suggestion is that, if you're a begginner, you should forget about it entirely. 我的建议是,如果您是初学者,则应该完全忘记它。 The proper way of removing elements from lists is using either list.remove (based on element equality) or list.pop (based on index) 从列表中删除元素的正确方法是使用list.remove (基于元素相等性)或list.pop (基于索引)


def word_jamble(word):
   word = list(word)
   shuffle(word)
   print ''.join(word)

Here, you're using a function to achieve to distinct tasks: shuffling a word, and printing it. 在这里,您正在使用一个函数来实现不同的任务:将单词混排并打印出来。 Generally, it's a good practice to make each function perform only a specific task - this leads to more reusable and organized code. 通常,使每个函数仅执行特定任务是一种好习惯-这会导致代码更可重用和更有条理。 Instead of printing the result inside the function, consider returning it, and printing it outside. 与其将结果打印在函数内部,不如考虑将其返回并在外部打印。


from random import shuffle
# some code
from random import choice

It's good practice to keep your imports together, and on the beggining of your program. 良好的做法是使导入始终保持一致,并保证程序的开始。 If you're importing two elements from the same module, you can separate them using a comma: 如果要从同一模块导入两个元素,则可以使用逗号分隔它们:

from random import shuffle, choice

Finally, since you want to repeat the game until there are no words left, you need to use a cycle: 最后,由于您要重复游戏直到没有剩余的单词,因此需要使用一个循环:

while len(word_list)>0: # can be written simply as "while len(word_list):"
    #your code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM