Hangman Game Script With Python
Hangman is a great game to program into Python. It can be extremely complex, displaying graphics, the number of guesses left in the secret word, a huge bank of available words picked at random and countless other elements. It can also be quite simple. Here we have a mix between the two.
There’s plenty you can do to improve, enhance and expand on what we’ve presented here. You can include a routine that returns an error if the user enters a number or character. You can include extra points for someone who guesses the entire word in one go rather than one letter at a time and you could perhaps add Chopin’s Funeral March should you lose the game; or something celebratory if you win.
Consider replacing the bank of words too. They’re found under the bank list, and could easily be swapped out for something more difficult. If you download www.github.com/dwyl/englishwords you can find a text document with over 466,000 words. Perhaps you could swap the words in the bank to instead read the contents of the text file:
HANGMAN.PY
We’ve made a Hangman game board (the gallows) out of characters that can be displayed in the IDLE Shell, along with a huge bank of words to randomly choose from.Hangman Game Script With Python |
class Hangman: def _ _ init _ _ (self,word): self.word = word self.missed _ letters = [] self.guessed _ letters = [] def guess(self,letter): if letter in self.word and letter not in self. guessed _ letters: self.guessed _ letters.append(letter) elif letter not in self.word and letter not in self.missed _ letters: self.missed _ letters.append(letter) else: return False return True def hangman _ over(self): return self.hangman _ won() or (len(self.missed _ letters) == 6) def hangman _ won(self): if ‘ _ ’ not in self.hide _ word(): return True return False def hide _ word(self): rtn = ‘’ for letter in self.word: if letter not in self.guessed _ letters: rtn += ‘ _ ’ else: rtn += letter return rtn def print _ game _ status(self): print (board[len(self.missed _ letters)]) print (‘Word: ‘ + self.hide _ word()) print (‘Letters Missed: ‘,) for letter in self.missed _ letters: print (letter,) print () print (‘Letters Guessed: ‘,) for letter in self.guessed _ letters: print (letter,) print () def rand _ word(): bank = ‘ability about above absolute accessible accommodation accounting beautiful bookstore calculator clever engaged engineer enough handsome refrigerator opposite socks interested strawberry backgammon anniversary confused dangerous entertainment exhausted impossible overweight temperature vacation scissors accommodation appointment decrease development earthquake environment brand environment necessary luggage responsible ambassador circumstance congratulate frequent’.split() return bank[random.randint(0,len(bank))] def main(): game = Hangman(rand _ word()) while not game.hangman _ over(): game.print _ game _ status() user _ input = input(‘\nEnter a letter: ‘) game.guess(user _ input) game.print _ game _ status() if game.hangman _ won(): print (‘\nCongratulations! You have won!!’) else: print (‘\nSorry, you have lost.’) print (‘The word was ‘ + game.word) print (‘\nGoodbye!\n’) if _ _ name _ _ == “_ _ main _ _”: main()
QUIT()
Since this is the last example in our Python code repository, we thought we’d go out with a bang and feature the hangman gallows being drawn with each incorrect guess of the word. Don’t worry if it looks misaligned in the text here, this is merely due to the differences between using the Python IDLE editor and pasting the code into a word processor (which formats things differently).There’s plenty you can do to improve, enhance and expand on what we’ve presented here. You can include a routine that returns an error if the user enters a number or character. You can include extra points for someone who guesses the entire word in one go rather than one letter at a time and you could perhaps add Chopin’s Funeral March should you lose the game; or something celebratory if you win.
Consider replacing the bank of words too. They’re found under the bank list, and could easily be swapped out for something more difficult. If you download www.github.com/dwyl/englishwords you can find a text document with over 466,000 words. Perhaps you could swap the words in the bank to instead read the contents of the text file:
def rand _ word(): with open(“/home/pi/Downloads/words.txt”, “rt”) as f: bank=f.readlines() return bank[random.randint(0,len(bank))]
0 Response to "Hangman Game Script With Python"
Post a Comment