Advertisement
Guest User

Untitled

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