Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 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. +---+
  39. | |
  40. O |
  41. /|\ |
  42. |
  43. |
  44. =========''', '''
  45.  
  46. +---+
  47. | |
  48. O |
  49. /|\ |
  50. / |
  51. |
  52. =========''', '''
  53. +---+
  54. | |
  55. O |
  56. /|\ |
  57. / \ |
  58. |
  59. =========''']
  60. words = 'Clint John Bill Holliday Bonny Clyde Billy Wyatt Revolver Rifle Shotgun Gatling Colt '.split()
  61.  
  62. def getRandomWord(wordList):
  63. wordIndex = random.randint(0, len(wordList) - 1)
  64. return wordList[wordIndex]
  65.  
  66. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  67. print(HANGMANPICS[len(missedLetters)])
  68. print()
  69.  
  70. print('Missed letters:', end=' ')
  71. for letter in missedLetters:
  72. print(letter, end=' ')
  73. print()
  74.  
  75. blanks = '_' * len(secretWord)
  76.  
  77. for i in range(len(secretWord)):
  78. if secretWord[i] in correctLetters:
  79. blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
  80.  
  81. for letter in blanks:
  82. print(letter, end=' ')
  83. print()
  84.  
  85. def getGuess(alreadyGuessed):
  86.  
  87. while True:
  88. print('Guess a letter.')
  89. guess = input()
  90. guess = guess.lower()
  91. if len(guess) != 1:
  92. print('Please enter a single letter.')
  93. elif guess in alreadyGuessed:
  94. print('You have already guessed that letter. Choose again.')
  95. elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  96. print('Please enter a LETTER.')
  97. else:
  98. return guess
  99.  
  100. def playAgain():
  101.  
  102. print('Do you want to play again? (yes or no)')
  103. return input().lower().startswith('y')
  104.  
  105.  
  106. print('H A N G M A N')
  107. missedLetters = ''
  108. correctLetters = ''
  109. secretWord = getRandomWord(words)
  110. gameIsDone = False
  111.  
  112. while True:
  113. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  114.  
  115.  
  116. guess = getGuess(missedLetters + correctLetters)
  117.  
  118. if guess in secretWord:
  119. correctLetters = correctLetters + guess
  120.  
  121. foundAllLetters = True
  122. for i in range(len(secretWord)):
  123. if secretWord[i] not in correctLetters:
  124. foundAllLetters = False
  125. break
  126. if foundAllLetters:
  127. print('Yes! The secret word is "' + secretWord + '"! You have won!')
  128. gameIsDone = True
  129. else:
  130. missedLetters = missedLetters + guess
  131.  
  132. if len(missedLetters) == len(HANGMANPICS) - 1:
  133. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  134. print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
  135. gameIsDone = True
  136.  
  137. if gameIsDone:
  138. if playAgain():
  139. missedLetters = ''
  140. correctLetters = ''
  141. gameIsDone = False
  142. secretWord = getRandomWord(words)
  143. else:
  144. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement