0

I'm working on a simple text based hangman game in python. I have a rough program running but I've encountered two minor problems:

  1. Why does the welcome message print twice?

  2. When the user inputs a letter not in the word two times in a row, the second time, the "nope, try again"-message does not display and current word does not display. The first time an incorrect letter is input, it works. Why doesn't it work the second time?


from random import randrange

class HangmanApp:

    def __init__(self, interface):

        self.infile = open("hangman.txt", "r")
        self.interface = textInterface()

        for line in self.infile:
            self.wordlist = line.split()
        self.secretword = self.wordlist[randrange(len(self.wordlist))]

        self.letter_list = list(self.secretword)



    #tests the user's guess

    def guess(self):

        self.new_list = ["_"]*len(self.secretword)

        #keep loop going as long as there are letters in the list
        while self.letter_list != ["_"]*len(self.letter_list):                

            self.guess = self.interface.guess()

            if self.guess in self.letter_list:

                while self.guess in self.letter_list:

                    i = self.letter_list.index(self.guess)

                    #replace that letter from the list with "_" 
                    self.letter_list[self.letter_list.index(self.guess)] = "_"

                    #add the chosen letter to a new list for display
                    self.new_list[i] = self.guess                                   

                    #print list with letter added
                    self.interface.correct(self.new_list)                           

            else:

                self.interface.incorrect(self.new_list)

                self.guess = self.interface.guess()

class textInterface:

    def __init__(self):
        print("Welcome to Hangman!")

    def guess(self):
        guess = input("Guess a letter! ")
        return guess

    def display (self, word):
        string = ' '.join(word)
        print(string)

    def incorrect(self, word):
        print("Nope, try again")
        self.display(word)

    def correct(self, word):
        print("Correct")
        self.display(word)


    def main():
        inter = textInterface()
        app = HangmanApp(inter)
        app.guess()
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 1
    Well the welcome message will print twice because you create two instances of the `textInterface` class. When you instantiate `HangmanApp` don't you want to assign the `interface` argument to `self.interface` rather than create a new instance? – lafras Mar 30 '11 at 15:16
  • 1
    I recommend you learn how to use a debugger, this greatly helps with these kinds of problems. – Björn Pollex Mar 30 '11 at 15:20
  • @Space_C0wb0y: Sorry, I haven't seen any mention of the debugger in the text that I'm using. Do you know of a decent tutorial for it? –  Mar 30 '11 at 15:27
  • You should do this from your IDE. If you don't use an IDE, [choose one](http://stackoverflow.com/questions/81584/). If you insist on command line, check [pdb](http://docs.python.org/library/pdb.html). – Björn Pollex Mar 30 '11 at 15:31

1 Answers1

0

The welcome message is printed twice because you're creating two instances of textInterface: one in main() and another inside HangmanApp.__init__(). I think you meant to do:

self.interface = interface

instead of

self.interface = textInterface()

Inside HangmanApp.guess(), after receiving an incorrect guess (reaching the else: clause) you have an extra guess prompt that is not needed---one that doesn't pass through your checking code. I think this is probably causing the issue with it not working the second time around.

Greg Haskins
  • 6,714
  • 2
  • 27
  • 22