Hangman game in Python

Capture

Hangman game Script:

def hangman(secretWord):
    '''
    secretWord: the secret word to guess.

    * The game will let the user know how many letters the secretWord have.
    * Ask the user to supply one guess (i.e. letter) per round.
    * The user will receive feedback whether their guess appears in the computers word.
    * After each round, the game will also display to the user what letters have been
      guessed so far, as well as letters that the user has not yet guessed.

    '''

    print 'Welcome to the Hangman Game!'
    print 'I am selecting a word which has ' + str(len(secretWord)) + ' letters long'
    print '-----------'
    secretWord = secretWord
    lettersGuessed = []
    mistakesMade = 8

    while mistakesMade > 0:
        availableLetters = getRestLetters(lettersGuessed)
        print 'You have ' + str(mistakesMade) + ' guesses left'
        print 'Available Letters: ' + availableLetters
        userGuess = raw_input('Please guess a letter: ')
        userGuess = userGuess.lower()
        if userGuess in availableLetters:
            lettersGuessed += [userGuess,]
            if userGuess in secretWord:
                if isWordGuessed(secretWord, lettersGuessed) == True:
                    print 'Good guess: ' + getGuessedWord(secretWord, lettersGuessed)
                    print '-----------'
                    return 'Congratulations, You won!'
                else:
                    print 'Good Guess: ' + getGuessedWord(secretWord, lettersGuessed)
            else:
                print 'Oops! That letter is not in my word: ' + getGuessedWord(secretWord, lettersGuessed)
                mistakesMade -= 1
        else:
            print "Oops! You've already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed)
        print '-----------'
    print 'Sorry, You lost! The word was ' + secretWord

What letters have not yet been guessed:

import string
def getRestLetters(lettersGuessed):
    '''
    lettersGuessed: what letters have been guessed so far
    '''
    res=''
    for e in string.ascii_lowercase:
        if e not in lettersGuessed:
            res=res+e
    return res

what letters in secretWord have been guessed so far:

import string
def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: the word the user is guessing
    lettersGuessed: what letters have been guessed so far
    '''
    res=''
    for e in secretWord:
        if e in lettersGuessed:
            res=res+e
        else:
            res=res+'_'
    return res

Find if a secret word contains all the characters from a list:

#True if all the letters of secretWord are in lettersGuessed; False otherwise
def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: the word the user is guessing
    lettersGuessed: what letters have been guessed so far
    '''
    return set(secretWord) <= set(lettersGuessed)

Test it for you secret word:

print hangman('*********')

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.