| | |
Computer picks a random word and player has to guess it.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
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.
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.
This is not very hard if you use the random module.
python Syntax (Toggle Plain Text)
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!"
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
There is an English dictionary file attached to:
http://www.daniweb.com/forums/post160495-8.html
The file contains one English word per line, so pick one at random.
http://www.daniweb.com/forums/post160495-8.html
The file contains one English word per line, so pick one at random.
May 'the Google' be with you!
Here is a random word generator for this dictionary file
Note that the file is opened but not read in memory. Only 25 characters are read at a time when a random word is requested.
python Syntax (Toggle Plain Text)
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 ).split ("\r\n") if len (L )>2 : return L [1 ] if __name__ =="__main__": n =100 print "%d randomly generated words"%n for i in xrange (n ): print random_word (), file_in .close ()
Last edited by Gribouillis; Nov 26th, 2008 at 3:46 am.
For a french version of the game, this site http://www.pallier.org/ressources/dicofr/dicofr.html gives a text file of 336531 french words
That 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
this will work though
here is something that will raise that error
python Syntax (Toggle Plain Text)
def add(x,y): z = x+y return z
python Syntax (Toggle Plain Text)
def add(x,y): z = x+y return z
Last edited by Paul Thompson; Nov 27th, 2008 at 2:36 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
So what is wrong with this code especially line 14 -
There's an error in your program *** 'return' outside function (random_word.py , line 14.)
There's an error in your program *** 'return' outside function (random_word.py , line 14.)
Python Syntax (Toggle Plain Text)
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 ).split ("\r\n") if len (L)>2 : return L [1] if __name__ =="__main__": n =100 print "%d randomly generated words"%n for i in xrange (n ): print random_word (), file_in .close ()
Last edited by dseto200; Nov 27th, 2008 at 3:36 pm.
see the indentation of line 14 and line 13 and line 12 and line 11 do not match the indentation needed to set them all inside the function. You just need to re-write this in a python editor and it should auto-indent it for you.
it should be
it should be
python Syntax (Toggle Plain Text)
def random_word (): while True: offset =randint (0 ,file_size ) file_in .seek (offset) L=file_in .read (25 ).split ("\r\n") if len (L)>2 : return L [1]
Last edited by Paul Thompson; Nov 27th, 2008 at 3:48 pm.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Other Threads in the Python Forum
- Previous Thread: Questions on How to Perform Certain Concepts
- Next Thread: wx.CallAfter()
Views: 1842 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache application argv beginner binary calculator character code command compile cursor cx-freeze development dictionary dynamic error event examples excel file float format ftp function google gui homework ideas import input java keyboard launcher line linux list lists loop microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext session shebang signal simple sprite ssh string strings table terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 valueerror variable verify voip windows wordgame wxpython






