This example describes a simple hangman game that Python implements. Share to everyone for your reference. Specifically as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 |
#!/usr/bin/env python import random Import Cpickle Class Hangman (object): "A Simple Hangman Game" tries to improve Your vocabulary a bit ' def __init__ (self): # The variables used, this isn't necessary self.dumpfile = ' #the Dictiona ry file Self.dictionary = {} #the pickled dict self.words = [] #list of words used = ' #the ' key ' Self.secret_word Ength = 0 #length of the ' key ' Self.keys = [] #inputs that match the ' key ' Self.used_keys = [] #keys that are already used self.guess = ' #player ' s guess self.mistakes = 0 #number of incorrect inputs return self.load_dict () #insert some random Hints for the player Def insert_random (self, length): Randint = 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 matches if self.guess in self.secret_word:indexes = [I for I, item in enumerate (Self.secret_word) If item = = Self.guess] For index in indexes:self.keys[index] = self.guess self.used_keys.append (self.guess) print "Used letters", set (self.used_ keys), ' n ' #if the guessed letter didn ' t match else:self.used_keys.append (self.guess) Self.mistakes + + 1 print "used Lette Rs ", set (Self.used_keys), ' n ' # load the pickled word dictionary and unpickle them def load_dict (self): Try:self.dumpfile = Open ("~/python/hangman/wordsdict.pkl", "R") except Ioerror:print "couldn ' t find the file ' Wordsdict.pkl '" Quit () self. Dictionary = cpickle.load (self.dumpfile) self.words = Self.dictionary.keys () self.dumpfile.close () return Self.prepare _word () #randomly Choose a word for the challenge Def Prepare_word (self): Self.secret_word = Random.choice (self.words) #do N ' 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 challenge def ask (self): print '. Join (Self.keys), ":", SELF.D Ictionary[self.secret_word] PRint return Self.input_loop () #take input to the player Def Input_loop (self): #four self.mistakes are allowed = Len (Set (Self.secret_word)) + 4 while chances!= 0 and Self.mistakes < 5:try:self.guess = Raw_input (">") except EO Ferror:exit (1) self.test_input () print '. Join (Self.keys) If ' _ ' not in Self.keys:print ' down done! ' break chances-= 1 If Self.mistakes > 4:print ' The word is ', '. Join (Self.secret_word). Upper () return Self.quit_message () def quit_mess Age (self): print "n" print "Press ' C ' to continue, or any of the other keys to quit the game." Print "Can always quit the GAM E 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__ () |
I hope this article will help you with your Python programming.