The example in this paper describes the simple hangman game implemented by Python. Share to everyone for your reference. Specific as follows:
#!/usr/bin/env pythonimport Random Import Cpickle Class Hangman (object): "A simple Hangman game, tries to improve Y Our vocabulary a bit "def __init__ (self): # The variables used, this is not necessary self.dumpfile = ' #the Dictionary file self.dictionary = {} #the pickled dict self.words = [] #list of words used Self.secret_wor d = ' #the ' key ' self.length = 0 #length of the ' key ' Self.keys = [] #inputs that match the ' key ' self . Used_keys = [] #keys that is already used self.guess = ' #player ' guess self.mistakes = 0 #number of In Correct inputs return self.load_dict () #insert Some random hints for the player Def insert_random (self, length): R Andint = random.randint # 3 hints if length >= 7:hint = 3 Else:hint = 1 for x in xrange (hint): a = Randint (1, length-1) self.keys[a-1] = Self.secret_word[a-1] def test_input (self): #if the guessed letter mat Ches if self.guess in Self.secret_word:indexes = [I for I, item in enumerate (Self.secret_word) if item = = Self.guess] for index in I Ndexes:self.keys[index] = self.guess self.used_keys.append (self.guess) print "Used letters", set (sel F.used_keys), ' \ n ' #if the guessed letter didn ' t match else:self.used_keys.append (self.guess) self.mistake s + = 1 print "Used letters", set (Self.used_keys), ' \ n ' # load the pickled word dictionary and unpickle them def Lo Ad_dict (self): Try:self.dumpfile = open ("~/python/hangman/wordsdict.pkl", "R") except Ioerror:print "Co Uldn ' t find the file ' Wordsdict.pkl ' "quit () Self.dictionary = Cpickle.load (self.dumpfile) self.words = Self.di Ctionary.keys () Self.dumpfile.close () return Self.prepare_word () #randomly Choose a word for the challenge def pre Pare_word (self): Self.secret_word = Random.choice (self.words) #don ' t count trailing spaces self.length = Len (self . Secret_word.rstrip ()) Self.keys = [' _ ' For x in Xrange (self.length)] Self.insert_random (self.length) return Self.ask () #display the CHA Llenge def ask (self): print ". Join (Self.keys),": ", Self.dictionary[self.secret_word] Print return Self.inpu T_loop () #take input from the player def Input_loop: #four self.mistakes is allowed chances = Len (set (self). Secret_word)) + 4 while chances! = 0 and Self.mistakes < 5:try:self.guess = Raw_input (">") Except Eoferror:exit (1) self.test_input () print ". Join (Self.keys) If ' _ ' Not in Self.keys: print ' Well done! ' Break Chances-= 1 if self.mistakes > 4:print ' The word was ', '. Join (Self.secret_word). Upper () return to self . Quit_message () def quit_message (self): print "\ n" print "Press ' C ' to continue, or any other key to quit the game. "Print" You can always quit the game by pressing ' ctrl+d ' "Try:command = Raw_input (' > ') if CoMmand = = ' C ': Return self.__init__ () #loopback else:exit (0) except Eoferror:exit (1) if __name__ = = ' __main__ ': Game = Hangman () game.__init__ ()
Hopefully this article will help you with Python programming.