0% found this document useful (0 votes)
16 views4 pages

Experiment - 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Experiment - 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment – 8

Write a program to implement Hangman game

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

someWords = '''apple banana mango strawberry orange grape pineapple apr


icot lemon coconut watermelon
cherry papaya berry peach lychee muskmelon'''

someWords = someWords.split(' ')


word = random.choice(someWords)

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

# Validation of the guess


if not guess.isalpha():
print('Enter only a LETTER')
continue
elif len(guess) > 1:
print('Enter only a SINGLE letter')
continue
elif guess in letterGuessed:
print('You have already guessed that letter')
continue

# If letter is guessed correcly


if guess in word:
letterGuessed += guess

# Print the word


for char in word:
if char in letterGuessed:
print(char, end = ' ')
correct += 1
else:
print('_', end = ' ')

# If user has guessed all the letters


if (Counter(letterGuessed) == Counter(word)):
print()
print('Congratulations, You won!')
chances = -1;
break

# If user has used all of his chances


if chances == 0:
print()
print('You lost! Try again..')
print('The word was {}'.format(word))

except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()

# print(letterGuessed)
Output

You might also like