Guest User

Untitled

a guest
Oct 18th, 2019
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. HANGMANPICS = ["""
  4.  
  5. +---+
  6. | |
  7. |
  8. |
  9. |
  10. |
  11. =========""", """
  12.  
  13. +---+
  14. | |
  15. O |
  16. |
  17. |
  18. |
  19. =========""", """
  20.  
  21. +---+
  22. | |
  23. O |
  24. | |
  25. |
  26. |
  27. =========""", """
  28.  
  29. +---+
  30. | |
  31. O |
  32. /| |
  33. |
  34. |
  35. =========""", """
  36.  
  37. +---+
  38. | |
  39. O |
  40. /|\ |
  41. |
  42. |
  43. =========""", """
  44.  
  45. +---+
  46. | |
  47. O |
  48. /|\ |
  49. / |
  50. |
  51. =========""", """
  52.  
  53. +---+
  54. | |
  55. O |
  56. /|\ |
  57. / \ |
  58. |
  59. ========="""]
  60.  
  61. words = {"Subjects":"english math science pe history art language health goverment economics".split(), "Sports":"football soccer basketball tennis cricket volleyball baseball track".split()}
  62.  
  63.  
  64. def getRandomWord(wordDict):
  65. wordKey = random.choice(list(wordDict.keys()))
  66. wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
  67. return [wordDict[wordKey][wordIndex], wordKey]
  68.  
  69. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  70. print(HANGMANPICS[len(missedLetters)])
  71. print()
  72.  
  73. print("Missed letters:", end=" ")
  74. for letter in missedLetters:
  75. print(letter, end=" ")
  76. print()
  77.  
  78. blanks = "_" * len(secretWord)
  79.  
  80. for i in range(len(secretWord)):
  81.  
  82. if secretWord[i] in correctLetters:
  83. blanks = blanks[:i] + secretWord[i] + blanks[i + 1:]
  84.  
  85. for letter in blanks:
  86. print(letter, end=" ")
  87. print()
  88.  
  89.  
  90. def getGuess(alreadyGuessed):
  91. while True:
  92. guess = input("Guess a letter. : ").lower()
  93. if len(guess) != 1:
  94. print("Please enter a single letter.")
  95. elif guess in alreadyGuessed:
  96. print("You have already that letter. Choose again.")
  97. elif guess not in "abcdefghijklmnopqrstuvwxyz":
  98. print("Please enter a LETTER.")
  99. else:
  100. return guess
  101.  
  102.  
  103. def playAgain():
  104. print("Do you want to play again? (Yes or no)")
  105. return input().lower().startswith("y")
  106.  
  107.  
  108. print("H A N G M A N")
  109. missedLetters = ""
  110. correctLetters = ""
  111. secretWord = getRandomWord(words)
  112. secretKey = secretWord[1]
  113. secretWord = secretWord[0]
  114. gameIsDone = False
  115.  
  116. while True:
  117. print("The secret word is in the set: " + secretKey)
  118. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  119. guess = getGuess(missedLetters + correctLetters)
  120. if guess in secretWord:
  121. correctLetters = correctLetters + guess
  122. if set(correctLetters) == set(secretWord):
  123. print("Yes! The secret word is " + secretWord + "! You have won!")
  124. gameIsDone = True
  125. else:
  126. missedLetters = missedLetters + guess
  127. print("missed it")
  128. if len(missedLetters) == len(HANGMANPICS) - 1:
  129. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  130. print("You have run out of guesses!\nAfter " + str(len(missedLetters)) + " missed guess and " + str(
  131. len(correctLetters)) + " correct guesses, the word was " + secretWord + ".")
  132. gameIsDone = True
  133.  
  134. if gameIsDone:
  135. if playAgain():
  136. missedLetters = ""
  137. correctLetters = ""
  138. gameIsDone = False
  139. secretWord = getRandomWord(words)
  140. else:
  141. break
RAW Paste Data