-->

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.

Hangman Game Script With Python


Hangman Game Script With Python


Hangman Game Script With Python


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
Hangman Game Script With Python


  1. class Hangman:
  2. def _ _ init _ _ (self,word):
  3. self.word = word
  4. self.missed _ letters = []
  5. self.guessed _ letters = []
  6. def guess(self,letter):
  7. if letter in self.word and letter not in self.
  8. guessed _ letters:
  9. self.guessed _ letters.append(letter)
  10. elif letter not in self.word and letter not in
  11. self.missed _ letters:
  12. self.missed _ letters.append(letter)
  13. else:
  14. return False
  15. return True
  16. def hangman _ over(self):
  17. return self.hangman _ won() or (len(self.missed _
  18. letters) == 6)
  19. def hangman _ won(self):
  20. if ‘ _ ’ not in self.hide _ word():
  21. return True
  22. return False
  23. def hide _ word(self):
  24. rtn = ‘’
  25. for letter in self.word:
  26. if letter not in self.guessed _ letters:
  27. rtn += ‘ _ ’
  28. else:
  29. rtn += letter
  30. return rtn
  31. def print _ game _ status(self):
  32. print (board[len(self.missed _ letters)])
  33. print (‘Word: ‘ + self.hide _ word())
  34. print (‘Letters Missed: ‘,)
  35. for letter in self.missed _ letters:
  36. print (letter,)
  37. print ()
  38. print (‘Letters Guessed: ‘,)
  39. for letter in self.guessed _ letters:
  40. print (letter,)
  41. print ()
  42. def rand _ word():
  43. bank = ‘ability about above absolute accessible
  44. accommodation accounting beautiful bookstore
  45. calculator clever engaged engineer enough
  46. handsome refrigerator opposite socks interested
  47. strawberry backgammon anniversary confused
  48. dangerous entertainment exhausted impossible
  49. overweight temperature vacation scissors
  50. accommodation appointment decrease development
  51. earthquake environment brand environment necessary
  52. luggage responsible ambassador circumstance
  53. congratulate frequent’.split()
  54. return bank[random.randint(0,len(bank))]
  55. def main():
  56.     game = Hangman(rand _ word())
  57.     while not game.hangman _ over():
  58.         game.print _ game _ status()
  59.         user _ input = input(‘\nEnter a letter: ‘)
  60.         game.guess(user _ input)
  61.         game.print _ game _ status()
  62.     if game.hangman _ won():
  63.         print (‘\nCongratulations! You have won!!’)
  64.     else:
  65.         print (‘\nSorry, you have lost.’)
  66.         print (‘The word was ‘ + game.word)
  67.         print (‘\nGoodbye!\n’)
  68.     if _ _ name _ _ == “_ _ main _ _”:
  69.         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.

Hangman Game Script With Python


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:

  1. def rand _ word():
  2.     with open(“/home/pi/Downloads/words.txt”, “rt”) as f:
  3.         bank=f.readlines()
  4.     return bank[random.randint(0,len(bank))]

0 Response to "Hangman Game Script With Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel