How do I get python to pick a random word? Do I have to link it with a dictionary? If so how do you do that.
Exercise from Python Programming for the absolute beginner
Create a game where computer picks a random word and player has to guess the word. Computer tells the player how many letter are in the word. The computer can only respond yes or no. Then the player must guess the word.
dseto200 0 Junior Poster in Training
Recommended Answers
Jump to PostThis is not very hard if you use the random module.
import random words = ['hello','python','monday','tuesday','friday','ice'] choice = random.choice(words) print "Guess the word!" guess = raw_input() while guess.lower() != choice: print "sorry, not correct" guess = raw_input() print "well dont that was it!"
Jump to PostThere is an English dictionary file attached to:
http://www.daniweb.com/forums/post160495-8.htmlThe file contains one English word per line, so pick one at random.
Jump to PostThat will mean that the return is not being properly recognised as inside of the function. This could be an indenting problem or just accidentally having a return statement somewhere else in your program that isnt a function.
here is something that will raise that error
Jump to PostInstead of the seek method, you could also use a dictionary to assign a number to each word, and then pick a random integer and print the word associated with that integer. IMHO it is preferable to use numbers whenever possible as that is the native language of the computer.
…
Jump to PostRan the program computer outputs 10 random words.
That's all the program was supposed to do. Now you will have to tell the user the number of letters, use a loop to ask for guesses, and compare to letters in the word with the appropriate correct/incorrect response.
All 17 Replies
lllllIllIlllI 178 Veteran Poster
dseto200 0 Junior Poster in Training
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague
dseto200 0 Junior Poster in Training
lllllIllIlllI 178 Veteran Poster
dseto200 0 Junior Poster in Training
lllllIllIlllI 178 Veteran Poster
dseto200 0 Junior Poster in Training
lllllIllIlllI 178 Veteran Poster
dseto200 0 Junior Poster in Training
lllllIllIlllI 178 Veteran Poster
dseto200 0 Junior Poster in Training
woooee 814 Nearly a Posting Maven
dseto200 0 Junior Poster in Training
woooee 814 Nearly a Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.