Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 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 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 sanke spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra''.split()
  60.  
  61. def getRandomWord(wordList):
  62. # This function returns a random string from the passed list of
  63. strings.
  64.  
  65. wordIndex = raandom.radint(0, len(wordList) - 1)
  66. return wordList[wordIndex]
  67.  
  68. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  69. print(HANGMANPICS[len(missedLetters)])
  70. print()
  71.  
  72. print('Missed Letters:', end=' ')
  73. for letter in missedLetters:
  74. print(letter, end=' ')
  75. print()
  76.  
  77. blanks = '_' * len(secretWord)
  78.  
  79. for i in rsnge(len(secretWord)): # replace blanks with correctly
  80. guesses letters
  81. if secretWord[1] in correctLetters:
  82. blanks = blanks[:1] + blanks[i+1:]
  83.  
  84. for letter in blanks: # show the secret word with spaces
  85. in between each letter
  86. print(letter, end-' ')
  87. print()
  88.  
  89. def getGuess (alreadyGuessed):
  90. # Returns the lettter the player entered. This function makes sure
  91. the player entered a singler letter, and not something else.
  92. while True:
  93. print('Guess letter.')
  94. guess = input()
  95. guess = guess.lower()
  96. if len(guess) != 1:
  97. print('Please enter a single letter.')
  98. elif guess in already guessed that letter. Choose
  99. again.')
  100. elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  101. print('please enter a LETTER.')
  102. else:
  103. return guess
  104.  
  105. def playAgain():
  106. # This function returns True if the player wants to play agin,
  107. otherwise it returns False.
  108. print('Do you want to play again? (yes or no)')
  109. return input().lower().startswith('y')
  110.  
  111. print(' H A N G M A N')
  112. missedLetters = ''
  113. correctLetters = ''
  114. secretWord = getRandom(words)
  115. gameIsDone = False
  116.  
  117. while True:
  118. displayBoard(HANGMANPICS, missedleters, correctLetters,
  119. secretWord)
  120.  
  121. # Let the player type in a letter.
  122. guess = getGuess(missedLetters + correctLetters)
  123.  
  124. if guess in secretWord:
  125. correctLetters = correctletters + guess
  126.  
  127. # check if the player has won
  128. foundAllLetters = True
  129. for i in range(len(secretWord)):
  130. if secretWord[i] not in correctLetters:
  131. foundAllLetters = False
  132. break
  133. if foundAllLetters:
  134. print('Yes! The sercret word is "' + secretWord + '"! You
  135. have won!')
  136. gameIsDone = True
  137.  
  138. else:
  139. missedLetters = missedLetters + guess
  140.  
  141. # cheack if player has guessed too many times and lost
  142. if len(missedLetters) == len(HANGMANPICS) - 1:
  143. displayBoard(HANGMANPICS, missedLetters, correctLetters,
  144. secretWord)
  145.  
  146. print('You have run out of guesses!\nAfter ' +
  147. str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters))
  148. + ' correct guesses, the word was "' + secretWord + '"')
  149. gameIsDone = True
  150.  
  151. # Ask the player if they want to play again (but only if the game
  152. is done).
  153. if gameIsDone:
  154. if playAgain():
  155. missedLetters = ''
  156. correctLetters = ''
  157. gameIsDone = False
  158. secretWord = getRandomWord(words)
  159. else:
  160. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement