Advertisement
Guest User

Untitled

a guest
Mar 18th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1.  
  2. HANGMANPICS = ['''
  3.  +---+
  4.  |   |
  5.      |
  6.      |
  7.      |
  8.      |
  9. =========''', '''
  10.  +---+
  11.  |   |
  12.  O   |
  13.      |
  14.      |
  15.      |
  16. =========''', '''
  17.  +---+
  18.  |   |
  19.  O   |
  20.  |   |
  21.      |
  22.      |
  23. =========''', '''
  24.  +---+
  25.  |   |
  26.  O   |
  27. /|   |
  28.      |
  29.      |
  30. =========''', '''
  31.  +---+
  32.  |   |
  33.  O   |
  34. /|\ |
  35.      |
  36.      |
  37. =========''', '''
  38.  +---+
  39.  |   |
  40.  O   |
  41. /|\ |
  42. /    |
  43.      |
  44. =========''', '''
  45.  +---+
  46.  |   |
  47.  O   |
  48. /|\ |
  49. / \ |
  50.      |
  51. =========''']
  52.  
  53. def pick_word():
  54.     """Return a random word from the word bank."""
  55.     words = ["soccer", "summer", "windows", "lights", "nighttime", "desktop", "walk"]
  56.     return random.choice(words)
  57.  
  58. def print_hangman(secret, guesses):
  59.     """Print the gallows, the man, and the blanked-out secret."""
  60.     wrong_guesses = [guess for guess in guesses if not guess in secret]
  61.     word_display = ' '.join(letter if letter in guesses else '_' for letter in secret)
  62.     print(HANGMANPICS[len(wrong_guesses)])
  63.     print()
  64.     print(word_display)
  65.  
  66. def guess(secret, guesses):
  67.     """Prompt for a single letter, append it to guesses, and return the guess."""
  68.     while True:
  69.         letter = input("Pick a letter: ")
  70.         if len(letter) != 1:
  71.             print("Please enter only one letter.")
  72.         elif letter not in 'abcdefghijklmnopqrstuvwxyz':
  73.             print("Please guess a letter.")
  74.         else:
  75.             guesses.append(letter)
  76.             return letter
  77.  
  78. def won(secret, guesses):
  79.     """Check whether the secret has been guessed."""
  80.     right_guesses = [letter for letter in secret if letter in guesses]
  81.     return len(right_guesses) >= len(secret)
  82.  
  83. def hanged(secret, guesses):
  84.     """Check whether too many guesses have been made."""
  85.     wrong_guesses = [guess for guess in guesses if not guess in secret]
  86.     return len(wrong_guesses) >= len(HANGMANPICS)
  87.  
  88. def play_hangman():
  89.     """Play one game of hangman. Return True if the player won."""
  90.     secret = pick_word()
  91.     guesses = []
  92.     message = None
  93.     while not hanged(secret, guesses):
  94.         print_hangman(secret, guesses)
  95.         if message is not None:
  96.             print()
  97.             print(message)
  98.         new_guess = guess(secret, guesses)
  99.         if won(secret, guesses):
  100.             print("Congratulations! You won and got the word", secret)
  101.             return True
  102.         elif new_guess in secret:
  103.             message = "Congratulations! {0} was found!".format(new_guess)
  104.         else:
  105.             message = "That letter is not in the word."
  106.     print("Sorry you lost! The correct word was", secret)
  107.     return False
  108.  
  109. def play_again():
  110.     while True:
  111.         play_again = input("Would you like to play again: ");
  112.         if play_again == "Y" or play_again == "y":
  113.             print("Creating a new game...")
  114.             return True
  115.         elif play_again == "N" or play_again == "n":
  116.             print("Thanks for playing, bye!")
  117.             return False
  118.         else:
  119.             print("Error: Please choose either 'Y' or 'N'")
  120.  
  121. while True:
  122.     play_hangman()
  123.     if not play_again():
  124.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement