Guest User

Untitled

a guest
Oct 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. import sys
  2. import getpass
  3. def hangman():
  4. print("Get ready to play Hangman!")
  5. word=getpass.getpass("Player 1, input your word: ").lower()
  6. #turns the user inputted word into a lower case in one line
  7. print('\n'*20)
  8. #Spaces out the inputted word
  9. start_game(word)
  10. #calls back the function start_game
  11. def start_game(word):
  12. player_lives = 6
  13. used_letters =[]
  14. number_dashes=["_" for i in range(len(word))]
  15. #Using list compression, set number_dashes to - - -....
  16. print(visuals(player_lives))
  17. print("")
  18. print("")
  19. print(" ".join(number_dashes))
  20. #using join function, turns '-','-' to - -
  21. while player_lives == player_lives:
  22. #loops forever
  23. input_letter =input("Player 2, input your letter guess: ")
  24. print('\n'*20)
  25. if len(input_letter) > 1:
  26. #checks if the letter in imutted is more than 2 letters
  27. player_lives-= 1
  28. print(visuals(player_lives))
  29. print(input_letter,"is not \'a letter\'!")
  30. print("Used letters:"," ".join(used_letters))
  31. print((updated_dashes(word,input_letter,number_dashes,)),'')
  32. #takes away a life and prints visuals, progress, and used letters
  33. elif input_letter not in used_letters and input_letter in word:
  34. #Checks if the letter is in the word, and not used already
  35. print(visuals(player_lives))
  36. print("Correct,",input_letter,"is in the word!")
  37. if input_letter not in used_letters:
  38. used_letters.append(str(input_letter))
  39. else:
  40. pass
  41. print("Used letters:"," ".join(used_letters))
  42. print((updated_dashes(word,input_letter,number_dashes,)),'')
  43. # prints new visuals, progress, and used letters
  44. elif input_letter not in word :
  45. #Checks if the letter guessed is not in the word
  46. player_lives-=1
  47. print(visuals(player_lives))
  48. print("Incorrect,",input_letter,"is not in the word")
  49. if input_letter not in used_letters:
  50. used_letters.append(str(input_letter))
  51. else:
  52. pass
  53. print("Used letters:"," ".join(used_letters))
  54. print((updated_dashes(word,input_letter,number_dashes,)),'')
  55. #takes away a life and prints new visuals, progress, and used letters
  56. elif input_letter in used_letters:
  57. #Finally checks if the letter is already used
  58. player_lives-=1
  59. print(visuals(player_lives))
  60. print("You already guessed",input_letter)
  61. if input_letter not in used_letters:
  62. used_letters.append(str(input_letter))
  63. else:
  64. pass
  65. print("Used letters:"," ".join(used_letters))
  66. print((updated_dashes(word,input_letter,number_dashes,)),'')
  67. # takes away a life and prints new visuals, progress, and used letters
  68. if player_lives == 0:
  69. you_lose(player_lives,word)
  70. #Checks if the player loses, then calls back function: you_lose
  71. elif word == "".join(number_dashes):
  72. you_win(player_lives,word)
  73. #Checks if the player wins, then calls back function: you_win
  74. def updated_dashes(word,input_letter,number_dashes):
  75. #everytime the player guesses a letter, it calls back thi function
  76. for i in range(len(word)):
  77. #loops based on length of word
  78. if input_letter == word[i]:
  79. #checks if the letter is equal to each letter individualy
  80. number_dashes[i] = input_letter
  81. #if so, sets the position in "- - -" to the inputted leter
  82. return (" ".join(number_dashes))
  83. #returns the updated progress "- - a" for example
  84. def you_lose(player_lives,word):
  85. #function is called back when player loses
  86. print("The guesser loses!","The word was",word)
  87. sys.exit()
  88. #prints what the word was then exits the program
  89. def you_win(player_lives,word):
  90. #function is called back when player wins
  91. print("The guesser wins!","The word was",word)
  92. sys.exit()
  93. #prints what the word was then exits the program
  94. def visuals(player_lives):
  95. #This function is used to print the progress through visuals
  96. if player_lives == 6:
  97. return"""
  98. _______
  99. | |
  100. |
  101. |
  102. |
  103. |
  104. |
  105. |________
  106. | |
  107. """
  108. elif player_lives == 5:
  109. return"""
  110. _______
  111. | |
  112. | O
  113. |
  114. |
  115. |
  116. |
  117. |________
  118. | |
  119. """
  120. elif player_lives == 4:
  121. return"""
  122. _______
  123. | |
  124. | O
  125. | --|
  126. |
  127. |
  128. |
  129. |________
  130. | |
  131. """
  132. elif player_lives == 3:
  133. return"""
  134. _______
  135. | |
  136. | O
  137. | --|--
  138. |
  139. |
  140. |
  141. |________
  142. | |
  143. """
  144. elif player_lives == 2:
  145. return"""
  146. _______
  147. | |
  148. | O
  149. | --|--
  150. | |
  151. | |
  152. |
  153. |________
  154. | |
  155. """
  156. elif player_lives == 1:
  157. return"""
  158. _______
  159. | |
  160. | O
  161. | --|--
  162. | |
  163. | | |
  164. |
  165. |________
  166. | |
  167. """
  168. elif player_lives == 0:
  169. return"""
  170. _______
  171. | |
  172. | l0
  173. | |||
  174. | |
  175. | | |
  176. |
  177. |________
  178. | D E A D|
  179. """
  180. hangman()
  181. #start of the code
Add Comment
Please, Sign In to add comment