943,955 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5503
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 28th, 2006
0

Guess a word

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 29th, 2006
0

Re: Guess a word

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 ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 29th, 2006
0

Re: Guess a word

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?
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 29th, 2006
0

Re: Guess a word

very nice! excellent little python project
Last edited by DarkFlash; Oct 29th, 2006 at 11:24 pm.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
DarkFlash is offline Offline
13 posts
since Oct 2006
Oct 30th, 2006
0

Re: Guess a word

Click to Expand / Collapse  Quote originally posted by LaMouche ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 31st, 2006
0

Re: Guess a word

*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?

python Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Nov 1st, 2006
0

Re: Guess a word

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 4th, 2006
0

Re: Guess a word

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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Zonr_0 is offline Offline
16 posts
since Oct 2006
Nov 4th, 2006
0

Re: Guess a word

Click to Expand / Collapse  Quote originally posted by Zonr_0 ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 5th, 2006
0

Re: Guess a word

Hmmm. True. I could even add in a dictionary of common 5-8 letter words or something.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python with bash shell
Next Thread in Python Forum Timeline: Loops in Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC