Member Avatar for Mouche

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.

Recommended Answers

All 14 Replies

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.

Member Avatar for Mouche

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?

very nice! excellent little python project

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.

Member Avatar for Mouche

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

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.

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.

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.

Member Avatar for Mouche

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

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?

Member Avatar for Mouche

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

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/

Member Avatar for Mouche

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.