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: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Computer picks a random word and player has to guess it.

 
0
  #1
Nov 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Computer picks a random word and player has to guess it.

 
0
  #2
Nov 25th, 2008
This is not very hard if you use the random module.
  1. import random
  2.  
  3. words = ['hello','python','monday','tuesday','friday','ice']
  4. choice = random.choice(words)
  5.  
  6. print "Guess the word!"
  7. guess = raw_input()
  8. while guess.lower() != choice:
  9. print "sorry, not correct"
  10. guess = raw_input()
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: Computer picks a random word and player has to guess it.

 
0
  #3
Nov 25th, 2008
But aren't you limiting to those list of words - hello, python, monday, tuesday, friday, and ice. How do you do for like a dictionary amount of words is it even possible?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Computer picks a random word and player has to guess it.

 
0
  #4
Nov 26th, 2008
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Computer picks a random word and player has to guess it.

 
0
  #5
Nov 26th, 2008
Here is a random word generator for this dictionary file
  1. from os.path import getsize
  2. from random import randint
  3.  
  4. dic_path ="DictionaryE.txt"
  5. file_size =getsize (dic_path )
  6. file_in =open (dic_path ,"rb")
  7.  
  8. def random_word ():
  9. while True :
  10. offset =randint (0 ,file_size )
  11. file_in .seek (offset )
  12. L =file_in .read (25 ).split ("\r\n")
  13. if len (L )>2 :
  14. return L [1 ]
  15.  
  16. if __name__ =="__main__":
  17. n =100
  18. print "%d randomly generated words"%n
  19. for i in xrange (n ):
  20. print random_word (),
  21. print
  22. file_in .close ()
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.
Last edited by Gribouillis; Nov 26th, 2008 at 3:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 984
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Computer picks a random word and player has to guess it.

 
0
  #6
Nov 26th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: Computer picks a random word and player has to guess it.

 
0
  #7
Nov 26th, 2008
Ran the above program I get the following error message "There's an error in your program ** ' return outside function (random_word.py, line 13.)
Line 13 - return L [1 ]
What does that message mean "return outside function"?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Computer picks a random word and player has to guess it.

 
0
  #8
Nov 27th, 2008
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
  1. def add(x,y):
  2. z = x+y
  3. return z
this will work though
  1. def add(x,y):
  2. z = x+y
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: Computer picks a random word and player has to guess it.

 
0
  #9
Nov 27th, 2008
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.)


  1. from os.path import getsize
  2. from random import randint
  3.  
  4. dic_path ="DictionaryE.txt"
  5. file_size =getsize (dic_path )
  6. file_in =open (dic_path ,"rb")
  7.  
  8. def random_word ():
  9. while True:
  10. offset =randint (0 ,file_size )
  11. file_in .seek (offset)
  12. L=file_in .read (25 ).split ("\r\n")
  13. if len (L)>2 :
  14. return L [1]
  15. if __name__ =="__main__":
  16.  
  17. n =100
  18.  
  19. print "%d randomly generated words"%n
  20.  
  21. for i in xrange (n ):
  22.  
  23. print random_word (),
  24. print
  25.  
  26. file_in .close ()
Last edited by dseto200; Nov 27th, 2008 at 3:36 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 147
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Computer picks a random word and player has to guess it.

 
0
  #10
Nov 27th, 2008
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
  1. def random_word ():
  2. while True:
  3. offset =randint (0 ,file_size )
  4. file_in .seek (offset)
  5. L=file_in .read (25 ).split ("\r\n")
  6. if len (L)>2 :
  7. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum


Views: 1842 | Replies: 17
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC