Guess a word

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Guess a word

 
0
  #1
Oct 28th, 2006
Another program. This one has you guess 5 letters and then gives you the word you're trying to guess as masked by -'s and has the letters you've guessed filled in.

Ex:
Say the word is "computer." If you guess "r," "s," "e," "a," and "m." It would say:
  1. --m--er
  2. Guess the word:


[php]
# import module for random functions
import random

# List of words for the computer to pick from
words = ("basketball", "football", "hockey", "lacrosse", "baseball")

# Word to be guessed; picked at random
word = random.choice(words)
letters_guessed = []

print "Guess the sport!"
print "You get to give five letters."
print "There are %s letters in the word." % (len(word))
guesses = 5
while guesses != 0:
letter = raw_input("Enter a letter: ")
if letter in letters_guessed:
print "You already guessed that letter."
else:
guesses = guesses - 1
print "You have %d guesses left." % (guesses)
letters_guessed.append(letter)


print "The word:"

masked_word = ""
for letter in word:
if letter in letters_guessed:
masked_word += letter
else: masked_word += "-"
print masked_word
guess = raw_input("Guess the word: ")
if guess == word:
print "Congratulations, %s is the word!" % (guess)
else:
print "Nope. The word is %s." % (word)
[/php]


Once again, could you good python programmers check out my program and see how it could be more efficient, more user-friendly, or changed. All of my programs are for learning purposes. Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,135
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: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Guess a word

 
0
  #2
Oct 29th, 2006
Wonderful game, could be the start of a hangman game. I have 2 suggestions. Make all the input lower case with lower() and let the user know if the letter guess was acceptable by updating the masked_word each time, easily done with a function ...
  1. # word guess game by LaMouche
  2. # import module for random functions
  3. import random
  4.  
  5. def word_update(word, letters_guessed):
  6. masked_word = ""
  7. for letter in word:
  8. if letter in letters_guessed:
  9. masked_word += letter
  10. else: masked_word += "-"
  11. print "The word:", masked_word
  12.  
  13. # List of words for the computer to pick from
  14. words = ("basketball", "football", "hockey", "lacrosse", "baseball")
  15.  
  16. # Word to be guessed; picked at random
  17. word = random.choice(words)
  18.  
  19. print "="*32
  20. print " Guess the sport!"
  21. print "You get to guess five letters."
  22. print "There are %s letters in the word." % (len(word))
  23. print "="*32
  24. guesses = 5
  25. letters_guessed = []
  26. while guesses != 0:
  27. # make the letter lower case with .lower()
  28. letter = raw_input("Enter a letter: ").lower()
  29. if letter in letters_guessed:
  30. print "You already guessed that letter."
  31. else:
  32. guesses = guesses - 1
  33. print "You have %d guesses left." % (guesses)
  34. letters_guessed.append(letter)
  35. word_update(word, letters_guessed)
  36.  
  37.  
  38. # again, make input lower case
  39. guess = raw_input("Guess the word: ").lower()
  40. if guess == word:
  41. print "Congratulations, %s is the word!" % (guess)
  42. else:
  43. print "Nope. The word is %s." % (word)
Do me a favor and put the idea into the "Projects for the Beginner" thread.
The finished product could be a contribution to the Python code snippets.
Last edited by vegaseat; Aug 13th, 2009 at 3:47 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Guess a word

 
0
  #3
Oct 29th, 2006
Thanks for the advice.

I'll put the idea into the projects for the beginner.

For a snippet, what else should I do? Add some comments?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: DarkFlash is an unknown quantity at this point 
Solved Threads: 2
DarkFlash's Avatar
DarkFlash DarkFlash is offline Offline
Newbie Poster

Re: Guess a word

 
0
  #4
Oct 29th, 2006
very nice! excellent little python project
Last edited by DarkFlash; Oct 29th, 2006 at 11:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,135
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: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Guess a word

 
0
  #5
Oct 30th, 2006
Originally Posted by LaMouche View Post
Thanks for the advice.

I'll put the idea into the projects for the beginner.

For a snippet, what else should I do? Add some comments?
Yes, just add a sentence or two explaining the purpose and features of your code.
If you do it from within the Python Code Snippets, then it will surely go to the Python language.
Last edited by vegaseat; Oct 30th, 2006 at 11:39 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Guess a word

 
0
  #6
Oct 31st, 2006
*post deleted*

Err... I started working on one thing, got distracted, and worked on another part. Well... I never finished the first thing and got an EOL error! Stuuuuuuupid.

EDIT:

Okay... finished copy. Worthy of becoming a code snippet?

  1. # Word Guess Game by LaMouche
  2. #
  3. # This game takes a random word from a list,
  4. # gives you five prompts to guess letters in the word,
  5. # and then it shows you a "masked word," filling
  6. # in the letters you've guessed.
  7. #
  8. # Say the word is "hockey" and the user guesses:
  9. # 'h,' 'e,' 't,' 'r,' and 's.' The masked word
  10. # shown would be h---e- . Then the user gets one
  11. # try to guess that word.
  12. #
  13. # More words can be added to the game by adding an
  14. # element into the "words" variable.
  15. #
  16. # import module for random functions
  17. import random
  18.  
  19. def word_update(word, letters_guessed): # update the masked word
  20. masked_word = ""
  21. for letter in word:
  22. if letter in letters_guessed:
  23. masked_word += letter
  24. else: masked_word += "-"
  25. print "The word:", masked_word
  26.  
  27. # List of words for the computer to pick from
  28. words = ("basketball", "football", "hockey", "lacrosse", "baseball")
  29.  
  30. # Word to be guessed; picked at random from "words"
  31. word = random.choice(words)
  32.  
  33. guesses = 5 # You can change this; it's how many letter guesses the user gets
  34. letters_guessed = []
  35.  
  36. print "="*32
  37. print " Guess the sport!"
  38. print "You get to guess %d letters." % (guesses)
  39. print "There are %s letters in the word." % (len(word))
  40. print "="*32
  41.  
  42. while guesses != 0:
  43. letter = raw_input("Enter a letter: ").lower()
  44. # Doesn't use up a guess if the user has already guessed that letter
  45. if letter in letters_guessed:
  46. print "You already guessed that letter."
  47. else:
  48. guesses = guesses - 1
  49. print "You have %d guesses left." % (guesses)
  50. letters_guessed.append(letter)
  51. word_update(word, letters_guessed)
  52. word_guesses = 1 # number of guesses to guess the word
  53. word_guess = 0 # current guess number
  54. if word_guesses == 1:
  55. print "You get 1 guess to guess the word."
  56. else:
  57. print "You get %d guesses to guess the word." % (word_guesses)
  58. while word_guess != word_guesses:
  59. guess = raw_input("Guess the word: ").lower()
  60. if guess == word:
  61. print "Congratulations, %s is the word!" % (guess)
  62. break
  63. else:
  64. print "Nope."
  65. word_guess += 1
  66. if word_guess == word_guesses:
  67. print "You ran out of tries!\nThe word was %s." % (word)
  68.  
  69. print "\nThanks for playing LaMouche's Word Guess Game."
Last edited by vegaseat; Aug 13th, 2009 at 3:48 pm. Reason: code=python tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,135
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: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Guess a word

 
0
  #7
Nov 1st, 2006
Works nicely! You should stick it into the Python Code Snippets.

For learning purposes I would add one more thing, loop the program so the player can play several words before finishing the game. That will add a little complexity, you don't want to repeat a word that has already been played.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Re: Guess a word

 
0
  #8
Nov 4th, 2006
Another possible challenge, have it able to access a text file from the computer containing a bunch of possible words, so the user can add their own if they wish.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,135
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: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Guess a word

 
0
  #9
Nov 4th, 2006
Originally Posted by Zonr_0 View Post
Another possible challenge, have it able to access a text file from the computer containing a bunch of possible words, so the user can add their own if they wish.
That would be a good addition. You could get the words from a file, or from a builtin list if the file is missing.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 134
Reputation: LaMouche is on a distinguished road 
Solved Threads: 20
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Guess a word

 
0
  #10
Nov 5th, 2006
Hmmm. True. I could even add in a dictionary of common 5-8 letter words or something.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum


Views: 3899 | Replies: 14
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC