Guest User

annotations of chapter 8 hangman

a guest
Jun 18th, 2026
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | Source Code | 0 0
  1. import random
  2. HANGMANPICS = ['''
  3.  
  4. +---+
  5. | |
  6. |
  7. |
  8. |
  9. |
  10. =========''', '''
  11.  
  12. +---+
  13. | |
  14. O |
  15. |
  16. |
  17. |
  18. =========''', '''
  19.  
  20. +---+
  21. | |
  22. O |
  23. | |
  24. |
  25. |
  26. =========''', '''
  27.  
  28. +---+
  29. | |
  30. O |
  31. /| |
  32. |
  33. |
  34. =========''', '''
  35.  
  36. +---+
  37. | |
  38. O |
  39. /|\ |
  40. |
  41. |
  42. =========''', '''
  43.  
  44. +---+
  45. | |
  46. O |
  47. /|\ |
  48. / |
  49. |
  50. =========''', '''
  51.  
  52. +---+
  53. | |
  54. O |
  55. /|\ |
  56. / \ |
  57. |
  58. =========''']
  59.  
  60. words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()
  61.  
  62. def getRandomWord(wordList):
  63. # This function returns a random string from the passed list of strings.
  64. wordIndex = random.randint(0, len(wordList) - 1)
  65. return wordList[wordIndex]
  66.  
  67. print('H A N G M A N')
  68. secretWord = getRandomWord(words)
  69. gameIsDone = False
  70.  
  71. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  72.  
  73. print(HANGMANPICS[len(missedLetters)])
  74. print()
  75.  
  76. print('Missed letters:', end=' ')
  77. for letter in missedLetters:
  78. print(letter, end=' ')
  79. print()
  80.  
  81. blanks = '_' * len(secretWord)
  82.  
  83. for i in range(len(secretWord)): # replace blanks with correctly guessed letters
  84. if secretWord[i] in correctLetters:
  85. blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
  86.  
  87. for letter in blanks: # show the secret word with spaces in between each letter
  88. print(letter, end=' ')
  89. print()
  90.  
  91. missedLetters = ''
  92. correctLetters = ''
  93. def getGuess(alreadyGuessed):
  94. # Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.
  95. while True:
  96. print('Guess a letter.')
  97. guess = input()
  98. guess = guess.lower()
  99. if len(guess) != 1:
  100. print('Please enter a single letter.')
  101. elif guess in alreadyGuessed:
  102. print('You have already guessed that letter. Choose again.')
  103. elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  104. print('Please enter a LETTER.')
  105. else:
  106. return guess
  107.  
  108. def playAgain():
  109. # This function returns True if the player wants to play again, otherwise it returns False.
  110. print('Do you want to play again? (yes or no)')
  111. return input().lower().startswith('y')
  112.  
  113.  
  114.  
  115.  
  116. while True:
  117. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  118.  
  119. # Let the player type in a letter.
  120. guess = getGuess(missedLetters + correctLetters)
  121.  
  122. if guess in secretWord: #if 1 input letter in sW...
  123. correctLetters = correctLetters + guess #cL display input and...
  124. # Check if the player has won
  125. foundAllLetters = True
  126. for i in range(len(secretWord)): #for EACH secretW letter...
  127. if secretWord[i] not in correctLetters: #if EACH secretW i NOT IN cor.Letter....
  128. foundAllLetters = False #wincon not activated... and 3.6...
  129. break
  130. if foundAllLetters: #else if EACH secretWord IS... you win
  131. print('Yes! The secret word is "' + secretWord + '"! You have won!')
  132. gameIsDone = True
  133. else:#else is outer block outside whiletrue, meaning it's what inner blocks aka for loop and if statement you didn't qualify for, defer to
  134. missedLetters = missedLetters + guess
  135.  
  136. # Check if player has guessed too many times and lost
  137. if len(missedLetters) == len(HANGMANPICS) - 1:
  138. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  139. print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
  140. gameIsDone = True
  141.  
  142. # Ask the player if they want to play again (but only if the game is done).
  143. if gameIsDone:
  144. if playAgain():
  145. missedLetters = ''
  146. correctLetters = ''
  147. gameIsDone = False
  148. secretWord = getRandomWord(words)
  149. else:
  150. break
  151.  
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment