This is a simple Hangman game using Python programming language
This is a simple Hangman game using Python programming language
#CODE
# Python Program to illustrate
# Hangman Game
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:
# For printing the empty spaces for letters of the word
print('_', end=' ')
print()
playing = True
# List for storing the letters guessed by the player
letterGuessed = ''
chances = len(word) + 2
correct = 0
flag = 0
try:
while (chances != 0) and flag == 0: # Flag is updated when the word is correctly guessed
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()
OUTPUT
Please run the program on your terminal.
EXAMPLE
Class 11 python project/Documents/
Python-Programs$ python P37_HangmanGame.py
Guess the word! HINT: word is a name of a fruit
_____
Enter a letter to guess: m
_ _m _ _
Enter a letter to guess: 0
__mo_
Enter a letter to guess: l
l _m o_ _
Enter a letter to guess: e
lemo_
Enter a letter to guess: n
lemon
Congratulations, You won!
Code Explanation:
1. The code starts by importing the random module.
2. This module provides a way to generate random numbers.
3. Next, the code creates someWords, which is a list of fruit names.
4. The list is split into spaces using the string ‘ ‘, and then each space is
replaced with a letter.
5. Next, the code randomly selects a secret word from our someWords
list.
6. This word will be used as the input for the game later on.
7. The next part of the code checks to see if the user has entered an
alpha character (a letter that appears in front of other letters).
8. If not, then they are asked to enter only a letter.
9. If they enter an alpha character, then it’s assumed that they want to
guess at another letter in word .
10. So, this part of the code checks to see if guess matches one of the
letters in word .
11. If it does, then chances is updated and flag is set to 1 .
12. Otherwise, chances is decreased by 1 and flag remains at 0 .
13. The next part of the code tries to guess at another letter in word .
14. If guess isn’t valid (i.e., it doesn’t match any of the letters in word ),
then print() prints out all empty spaces for letters in word , and
15. The code starts by importing the random module.
16. This module provides us with a number of useful functions, one of
which is the choice function.
17. This function allows us to randomly choose a secret word from our
list of words.
18. Next, we create some variables which will be used throughout the
program.
19. These include someWords , word and letterGuessed .
20. letterGuessed will store the letter guessed by the player, while
chances will store the number of times that the player has correctly
guessed the word so far.
21. correct will keep track of how many letters have been guessed so
far and flag will indicate whether or not the player has guessed the
word correctly.
22. We then start looping through our list of words and randomly
choosing a secret word from it