Search code examples
pythonloopsfor-loopwhile-loopnested-loops

Python Hangman - Loops


I am trying to write a function to use to play hangman, and everything is working perfectly except for the fact that the function doesn't recognize when the game has been won.

---EDIT---

here is my revised code

def hangman():
    word = choose_word(wordlist)
    guessed_word = len(word)*['_']
    guesses = 10
    available_letters = "abcdefghijklmnopqrstuvwxyz"
    guessed_letters = ""
    letters_correct = 0
    print "Welcome to the game, Hangman!"
    print "I am thinking of a word that is", len(word), "letters long."
    print "Available letters:", available_letters
    print "You have", guesses, "guesses."
    while letters_correct != len(word):
        guess = raw_input("enter your guess:")
        if len(guess)==1 and guess.isalpha():
            if guessed_letters.find(guess) != -1:
                print "You already picked", guess
            else:
                guessed_letters = guessed_letters + guess
                index1 = word.find(guess)
                if  index1 == -1:
                    print "The letter",guess,"is not in the word", ' '.join(guessed_word)
                    guesses = guesses - 1
                    print "You have", guesses, "guesses left."
                    if guesses == 0:
                        return "You are out of guesses. You lose. The word was "+ word + "."
                    available_letters = available_letters.replace(guess, '')
                    print "Available letters:", available_letters
                else:
                    letters_correct = letters_correct + word.count(guess)
                    print"The letter", guess, "is in the word."
                    for i in range(len(word)):
                        if guess == word[i]:
                            guessed_word[i] = guess
                            print ' '.join(guessed_word)
                            if letters_correct != len(word):
                                print "You have", guesses, "guesses left."
                                available_letters = available_letters.replace(guess, '')
                                print "Available letters:", available_letters
                            elif guesses <= 0:
                                return "You are out of guesses. You lose. The word was "+ word + "."
        else:
            print "Please guess a single letter in the alphabet."
    if letters_correct == len(word):
        return "Congratualations! You figured out that the word is "+ word

Solution

  • The reason for the problem with the hangman game loop not terminating is with the letters_correct counter variable that the game while loop checks.

    while letters_correct != len(word):
    

    Let's say the word is banana and I guess the letter a

    the branch in your code that handles a correct guess begins with this line:

    letters_correct = letters_correct + 1
    

    In the case of my word banana and guess a then the letters_correct variable should be incremented three times. Now if i guess n the letters_correct should be incremented twice.

    In your current implementation guessing in the order I did would only increment letters_correct twice (2), when it should be 5. So, when increasing letters_count increment it the number of times guess appears in word.

    letters_count = letters_count + word.count(guess)
    

    That way on the game loop check the letters_count variable would actually be able to reach the length of the word.