Write a program that plays a game of Hangman. Hangman is a two-player game in which one player, in our case, the program, picks a word (you can hardcode it) that the other player (the user) has to guess. The program originally reveals only the first and the last letters, and dashes for every other letter, e.g. N------K, if the word is NOTEBOOK. At each turn of the game, the user suggests a letter and the program responds by either stating that the word does not have the letter, or revealing the letter in the word, e.g. o in NO---OOK.

The game stops when either

  • user guesses the entire word, and then user is the winner, or
  • when user has proposed 7 letters not found in the word. In this case, the program wins.

Here's a sample run of the program:

Let's play a game of Hangman.
I have picked a word that starts with letter T and ends with E.
Here's the template in which each dash denotes a single letter:
T - - - - - - - - - E
Now it's your turn to guess a letter.

Please enter your next guess: F
Incorrect. Letter F does not occur in this word.

Please enter your next guess: E
Correct.
T E - - E - - - - - E

Please enter your next guess: Z
Incorrect. Letter Z does not occur in this word.

Please enter your next guess: P
Correct.
T E - P E - - - - - E

Please enter your next guess: Q
Incorrect. Letter Q does not occur in this word.

Please enter your next guess: O
Incorrect. Letter O does not occur in this word.

Please enter your next guess: L
Incorrect. Letter L does not occur in this word.

Please enter your next guess: I
Incorrect. Letter I does not occur in this word.

You lost this game. The word is TEMPERATURE
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.