Experiment - 8
Experiment - 8
Hangman Game is a guessing game for two or more players. One player thinks of a
word, phrase or sentence and the other tries to guess it by suggesting letters within a
certain number of guesses.
The word to guess is represented by a row of dashes, representing each letter of the
word.
If the guessing player suggests a letter which occurs in the word, the other player writes
it in all its correct positions.
If the suggested letter does not occur in the word, the other player draws one element of
a hanged man stick figure as a tally mark.
The player guessing the word may, at any time, attempt to guess the whole word. If the
word is correct, the game is over and the guesser wins.
Otherwise, the other player may choose to penalize the guesser by adding an element to
the diagram.
On the other hand, if the other player makes enough incorrect guesses to allow his
opponent to complete the diagram, the game is also over, this time with the guesser
losing.
Implementation of Hangman Game
We will write this Hangman game only for fruits. We only asked the user to guess the
word which is only of fruit category. So like the original game, we ask the user to guess
the character or word each time until either he lost all his chances or either wins the
game. We are going to give 2 extra chances to predict the correct letter of the word that
means total number of chances given to the user is equal to the length of word + 2 more
to it. So if the user has guessed all the letters correctly, we simply print the
Congratulations message to our terminal.
import random
from collections import Counter
if __name__ == '__main__':
print('Guess the word! HINT: word is a name of a fruit')
for i in word:
print('_', end = ' ')
print()
playing = True
letterGuessed = ''
chances = len(word) + 2
correct = 0
try:
while (chances != 0):
print()
chances -= 1
try:
guess = str(input('Enter a letter to guess: '))
except:
print('Enter only a letter!')
continue
except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()
# print(letterGuessed)
Output