| | |
Guess a word
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
[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.
Ex:
Say the word is "computer." If you guess "r," "s," "e," "a," and "m." It would say:
Python Syntax (Toggle Plain Text)
--m--er 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.
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 ...
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.
python Syntax (Toggle Plain Text)
# word guess game by LaMouche # import module for random functions import random def word_update(word, letters_guessed): masked_word = "" for letter in word: if letter in letters_guessed: masked_word += letter else: masked_word += "-" print "The word:", masked_word # 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) print "="*32 print " Guess the sport!" print "You get to guess five letters." print "There are %s letters in the word." % (len(word)) print "="*32 guesses = 5 letters_guessed = [] while guesses != 0: # make the letter lower case with .lower() letter = raw_input("Enter a letter: ").lower() 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) word_update(word, letters_guessed) # again, make input lower case guess = raw_input("Guess the word: ").lower() if guess == word: print "Congratulations, %s is the word!" % (guess) else: print "Nope. The word is %s." % (word)
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!
•
•
•
•
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?
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!
*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?
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)
# Word Guess Game by LaMouche # # This game takes a random word from a list, # gives you five prompts to guess letters in the word, # and then it shows you a "masked word," filling # in the letters you've guessed. # # Say the word is "hockey" and the user guesses: # 'h,' 'e,' 't,' 'r,' and 's.' The masked word # shown would be h---e- . Then the user gets one # try to guess that word. # # More words can be added to the game by adding an # element into the "words" variable. # # import module for random functions import random def word_update(word, letters_guessed): # update the masked word masked_word = "" for letter in word: if letter in letters_guessed: masked_word += letter else: masked_word += "-" print "The word:", masked_word # List of words for the computer to pick from words = ("basketball", "football", "hockey", "lacrosse", "baseball") # Word to be guessed; picked at random from "words" word = random.choice(words) guesses = 5 # You can change this; it's how many letter guesses the user gets letters_guessed = [] print "="*32 print " Guess the sport!" print "You get to guess %d letters." % (guesses) print "There are %s letters in the word." % (len(word)) print "="*32 while guesses != 0: letter = raw_input("Enter a letter: ").lower() # Doesn't use up a guess if the user has already guessed that 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) word_update(word, letters_guessed) word_guesses = 1 # number of guesses to guess the word word_guess = 0 # current guess number if word_guesses == 1: print "You get 1 guess to guess the word." else: print "You get %d guesses to guess the word." % (word_guesses) while word_guess != word_guesses: guess = raw_input("Guess the word: ").lower() if guess == word: print "Congratulations, %s is the word!" % (guess) break else: print "Nope." word_guess += 1 if word_guess == word_guesses: print "You ran out of tries!\nThe word was %s." % (word) print "\nThanks for playing LaMouche's Word Guess Game."
Last edited by vegaseat; Aug 13th, 2009 at 3:48 pm. Reason: code=python tags
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.
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!
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!
![]() |
Similar Threads
- Starting Python (Python)
- Word Scramble Game (Posting Games)
- POLL: Your Favorite Daniweb Word Game (Geeks' Lounge)
- Help, record arrays (VB.NET)
Other Threads in the Python Forum
- Previous Thread: python with bash shell
- Next Thread: Loops in Python
Views: 3899 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app assignment bash beginner bits bluetooth calling chmod cmd code convert data decimals dictionary dynamic dynamically examples excel external file float format ftp function gnu gui homework http import input itunes jaunty java keycontrol leftmouse line linux list lists loan loop maintain millimeter module mouse newb number numbers output parsing path pointer port prime program programming projects push py-mailer py2exe pygame pyqt python random recursion recursive scrolledtext slicenotation smtp split ssh string strings table tennis terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice windows wxpython xlib






