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 PostHere is a random word generator for this dictionary file
from os.path import getsize from random import randint dic_path ="DictionaryE.txt" file_size =getsize (dic_path ) file_in =open (dic_path ,"rb") def random_word (): while True : offset =randint (0 ,file_size ) file_in .seek (offset ) L =file_in .read (25 …
Jump to PostFor a french version of the game, this site http://www.pallier.org/ressources/dicofr/dicofr.html gives a text file of 336531 french words :)
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
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.