Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. """Hangman
  2. Standard game of Hangman. A word is chosen at random from a list and the
  3. user must guess the word letter by letter before running out of attempts."""
  4.  
  5. import random
  6.  
  7. def main():
  8. welcome = ['Welcome to Hangman! A word will be chosen at random and',
  9. 'you must try to guess the word correctly letter by letter',
  10. 'before you run out of attempts. Good luck!'
  11. ]
  12.  
  13. for line in welcome:
  14. print(line, sep='n')
  15.  
  16. # setting up the play_again loop
  17.  
  18. play_again = True
  19.  
  20. while play_again:
  21. # set up the game loop
  22.  
  23. words = ["hangman", "chairs", "backpack", "bodywash", "clothing",
  24. "computer", "python", "program", "glasses", "sweatshirt",
  25. "sweatpants", "mattress", "friends", "clocks", "biology",
  26. "algebra", "suitcase", "knives", "ninjas", "shampoo"
  27. ]
  28.  
  29. chosen_word = random.choice(words).lower()
  30. player_guess = None # will hold the players guess
  31. guessed_letters = [] # a list of letters guessed so far
  32. word_guessed = []
  33. for letter in chosen_word:
  34. word_guessed.append("-") # create an unguessed, blank version of the word
  35. joined_word = None # joins the words in the list word_guessed
  36.  
  37. HANGMAN = (
  38. """
  39. -----
  40. | |
  41. |
  42. |
  43. |
  44. |
  45. |
  46. |
  47. |
  48. --------
  49. """,
  50. """
  51. -----
  52. | |
  53. | 0
  54. |
  55. |
  56. |
  57. |
  58. |
  59. |
  60. --------
  61. """,
  62. """
  63. -----
  64. | |
  65. | 0
  66. | -+-
  67. |
  68. |
  69. |
  70. |
  71. |
  72. --------
  73. """,
  74. """
  75. -----
  76. | |
  77. | 0
  78. | /-+-
  79. |
  80. |
  81. |
  82. |
  83. |
  84. --------
  85. """,
  86. """
  87. -----
  88. | |
  89. | 0
  90. | /-+-
  91. |
  92. |
  93. |
  94. |
  95. |
  96. --------
  97. """,
  98. """
  99. -----
  100. | |
  101. | 0
  102. | /-+-
  103. | |
  104. |
  105. |
  106. |
  107. |
  108. --------
  109. """,
  110. """
  111. -----
  112. | |
  113. | 0
  114. | /-+-
  115. | |
  116. | |
  117. |
  118. |
  119. |
  120. --------
  121. """,
  122. """
  123. -----
  124. | |
  125. | 0
  126. | /-+-
  127. | |
  128. | |
  129. | |
  130. |
  131. |
  132. --------
  133. """,
  134. """
  135. -----
  136. | |
  137. | 0
  138. | /-+-
  139. | |
  140. | |
  141. | |
  142. | |
  143. |
  144. --------
  145. """,
  146. """
  147. -----
  148. | |
  149. | 0
  150. | /-+-
  151. | |
  152. | |
  153. | | |
  154. | |
  155. |
  156. --------
  157. """,
  158. """
  159. -----
  160. | |
  161. | 0
  162. | /-+-
  163. | |
  164. | |
  165. | | |
  166. | | |
  167. |
  168. --------
  169. """)
  170.  
  171. print(HANGMAN[0])
  172. attempts = len(HANGMAN) - 1
  173.  
  174.  
  175. while (attempts != 0 and "-" in word_guessed):
  176. print(("nYou have {} attempts remaining").format(attempts))
  177. joined_word = "".join(word_guessed)
  178. print(joined_word)
  179.  
  180. try:
  181. player_guess = str(input("nPlease select a letter between A-Z" + "n> ")).lower()
  182. except: # check valid input
  183. print("That is not valid input. Please try again.")
  184. continue
  185. else:
  186. if not player_guess.isalpha(): # check the input is a letter. Also checks an input has been made.
  187. print("That is not a letter. Please try again.")
  188. continue
  189. elif len(player_guess) > 1: # check the input is only one letter
  190. print("That is more than one letter. Please try again.")
  191. continue
  192. elif player_guess in guessed_letters: # check it letter hasn't been guessed already
  193. print("You have already guessed that letter. Please try again.")
  194. continue
  195. else:
  196. pass
  197.  
  198. guessed_letters.append(player_guess)
  199.  
  200. for letter in range(len(chosen_word)):
  201. if player_guess == chosen_word[letter]:
  202. word_guessed[letter] = player_guess # replace all letters in the chosen word that match the players guess
  203.  
  204. if player_guess not in chosen_word:
  205. attempts -= 1
  206. print(HANGMAN[(len(HANGMAN) - 1) - attempts])
  207.  
  208. if "-" not in word_guessed: # no blanks remaining
  209. print(("nCongratulations! {} was the word").format(chosen_word))
  210. else: # loop must have ended because attempts reached 0
  211. print(("nUnlucky! The word was {}.").format(chosen_word))
  212.  
  213. print("nWould you like to play again?")
  214.  
  215. response = input("> ").lower()
  216. if response not in ("yes", "y"):
  217. play_again = False
  218.  
  219. if __name__ == "__main__":
  220. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement