1.11M Members

Guess a word

 
0
 

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:

--m--er
Guess the word:
# 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)

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.

 
0
 

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 ...

# 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)

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.

 
0
 

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?

 
0
 

very nice! excellent little python project

 
0
 

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.

 
0
 

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

# 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."
 
0
 

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.

 
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.

 
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.

 
0
 

Hmmm. True. I could even add in a dictionary of common 5-8 letter words or something.

 
0
 

LaMouche, nice game!

What would a dictionary do to improve it?

Would it be hard to dress the game up in a GUI like Tkinter?

 
0
 

Umm.. the dictionary would just add a bunch more words for the computer to pick from. Meaning...if you played it a bunch of times and I added an option to play the game agaion, then once you got through the sports, you'd have more words to guess.

It'd be very difficult since I only know how to make a label and a useless button in Tkinter. ;)

 
0
 

For your information, a few nice folks are working on a 'Python Tkinter GUI Builder' (ptkgb), that allows you to drag and drop Tkinter widgets on one form similar to the Delphi RAD or MS Visual stuff. Take a look at:
http://sourceforge.net/projects/ptkgb/

 
0
 

I couldn't get it to work and then realizeed that you need python 2.4; 2.5 doesn't work. oh well...

 
0
 

I think basic Tkinter GUI coding is easy to learn. You don't need a builder, just get some sample code and a good tutorial and play with it.

I like the Tkinter tutorial at:
http://bembry.org/technology/python/index.php

Question Answered as of 4 Years Ago by vegaseat, bumsfeld, sneekula and 2 others
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article