Word Guess Game

Mouche 0 Tallied Votes 274 Views Share

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. More guesses can also be added. (See comments)

# Word Guess Game by LaMouche
# Finished October 30th, 2006

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