user3161743
user3161743

Reputation: 171

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))]

Upvotes: 0

Views: 3200

Answers (4)

loopbackbee
loopbackbee

Reputation: 23342

There are a few basic problems with the code:

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

list is the list constructor. You should never name your variables after python keywords.


del list[index(choice(l))]

del is very rarely needed in python. 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)


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

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239693

The main problem is the variable's name, list. Its a builtin type constructor's name. When you use say list, it shadows the builtin type's name. Apart from that, you can use pop method, like this, to get the random words out of the list easily

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

Upvotes: 1

Karl Knechtel
Karl Knechtel

Reputation: 61654

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. A name only names one thing at a time.

Upvotes: 0

tayfun
tayfun

Reputation: 3135

You should change your first variable, list, to something else. It is being confused with the built-in list type, and your list object is of course not callable.

Upvotes: 2

Related Questions