Advertisement
Guest User

Untitled

a guest
May 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. # Hangman Game
  2. #
  3. # The classic game of Hangman.  The computer picks a random word
  4. # and the player wrong to guess it, one letter at a time.  If the player
  5. # can't guess the word in time, the little stick figure gets hanged.
  6.  
  7. # imports
  8. import random
  9.  
  10. # constants
  11. HANGMAN = (
  12. """
  13. ------
  14. |    |
  15. |
  16. |
  17. |
  18. |
  19. |
  20. |
  21. |
  22. ----------
  23. """,
  24. """
  25. ------
  26. |    |
  27. |    O
  28. |
  29. |
  30. |
  31. |
  32. |
  33. |
  34. ----------
  35. """,
  36. """
  37. ------
  38. |    |
  39. |    O
  40. |   -+-
  41. |
  42. |  
  43. |  
  44. |  
  45. |  
  46. ----------
  47. """,
  48. """
  49. ------
  50. |    |
  51. |    O
  52. |  /-+-
  53. |  
  54. |  
  55. |  
  56. |  
  57. |  
  58. ----------
  59. """,
  60. """
  61. ------
  62. |    |
  63. |    O
  64. |  /-+-/
  65. |  
  66. |  
  67. |  
  68. |  
  69. |  
  70. ----------
  71. """,
  72. """
  73. ------
  74. |    |
  75. |    O
  76. |  /-+-/
  77. |    |
  78. |  
  79. |  
  80. |  
  81. |  
  82. ----------
  83. """,
  84. """
  85. ------
  86. |    |
  87. |    O
  88. |  /-+-/
  89. |    |
  90. |    |
  91. |   |
  92. |   |
  93. |  
  94. ----------
  95. """,
  96. """
  97. ------
  98. |    |
  99. |    O
  100. |  /-+-/
  101. |    |
  102. |    |
  103. |   | |
  104. |   | |
  105. |  
  106. ----------
  107. """)
  108. print "Welcome to Hangman.  Good luck!"
  109. MAX_WRONG = len(HANGMAN) - 1
  110. word_list = []
  111.  
  112. use_own_words = raw_input("would you like to add any of your own words? [y/n]")
  113. use_own_words = use_own_words[0]
  114. use_own_words = use_own_words.upper()
  115. dont_add_word = "false"
  116. if use_own_words == "Y":
  117.     own_word = raw_input("type a word to add or leave blank to finish adding ")
  118.     while own_word != "":
  119.         own_word = own_word.upper()
  120.         for i in range(len(word_list)):
  121.             if own_word == word_list[i]:
  122.                 dont_add_word = "true"
  123.         if dont_add_word == "false":
  124.             print "added"
  125.             word_list.append(own_word)
  126.         else:
  127.             print "you already added that word"
  128.             dont_add_word = "false"
  129.         own_word = raw_input("next word ")
  130.  
  131. guess = raw_input("\nEnter your guess: ")
  132.        
  133.  
  134.  
  135.  
  136. # initialize variables
  137. if word_list == "":
  138.     WORDS = ("OVERUSED", "CLAM", "GUAM", "PUCK", "TAFFETA", "PYTHON")
  139. else:
  140.     WORDS = word_list
  141. word = random.choice(WORDS)   # the word to be guessed
  142. #word = WORDS[4]  # For debug purposes only    TODO  :  Remove
  143. so_far = "-" * len(word)      # one dash for each letter in word to be guessed
  144. wrong = 0                     # number of wrong guesses player has made
  145. used = []                     # letters already guessed
  146.  
  147.  
  148.  
  149.  
  150. while wrong < MAX_WRONG and so_far != word:
  151.     print HANGMAN[wrong]
  152.     print "\nYou've used the following letters:\n", used
  153.     print "\nSo far, the word is:\n", so_far
  154.  
  155.     guess = raw_input("\n\nEnter your guess: ")
  156.     while guess == "":
  157.         guess = raw_input("\n\nEnter your guess: ")
  158.     guess = guess[0]
  159.     guess = guess.upper()
  160.    
  161.     while guess in used:
  162.         print "You've already guessed the letter:", guess
  163.         guess = raw_input("Enter your guess: ")
  164.         guess = guess.upper()
  165.  
  166.     used.append(guess)
  167.  
  168.     if guess in word:
  169.         print "\nYes!", guess, "is in the word!"
  170.        
  171.         # create a new so_far to include guess
  172.         new = ""
  173.         for i in range(len(word)):
  174.             if guess == word[i]:
  175.                 new += guess
  176.             else:
  177.                 new += so_far[i]              
  178.         so_far = new
  179.         print HANGMAN[wrong]
  180.         print "\nYou've used the following letters:\n", used
  181.         print "\nSo far, the word is:\n", so_far
  182.         answer = raw_input("would you like to guess the word? [y/n]")
  183.         answer = answer.upper()
  184.         if answer[0] == "Y":
  185.             guess_word = raw_input("enter word: ")
  186.             guess_word = guess_word.upper()
  187.             if word == guess_word:
  188.                 so_far = guess_word
  189.             else:
  190.                 print "\nSorry,", guess_word, "isn't the word."
  191.                 wrong += 1
  192.  
  193.     else:
  194.         print "\nSorry,", guess, "isn't in the word."
  195.         wrong += 1
  196.  
  197. if wrong >= MAX_WRONG:
  198.     print HANGMAN[wrong]
  199.     print "\nYou've been hanged!"
  200. else:
  201.     print "\nYou guessed it! \o,"
  202.    
  203. print "\nThe word was", word
  204.  
  205. raw_input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement