Hangman Wiki:
The origins of Hangman are obscure meaning not discovered, but it seems to have arisen in Victorian times, ” says Tony Augarde, author of The Oxford Guide to Word Games. The game is mentioned in Alice Bertha Gomme’s “Traditional Games” in 1894 under the name “Birds, Beasts and Fishes.” The rules are simple; a player writes down the first and last letters of a word and another player guesses the letters in between. In other sources, [where?] the game is called “Gallows”, “The Game of Hangin”, or “Hanger”.
Implementation:
This is a simple Hangman game using Python programming language. Beginners can use this as a small project to boost their programming skills and understanding logic.
- The Hangman program randomly selects a secret word from a list of secret words. The random module will provide this ability, so line 1 in program imports it.
- The Game: Here, a random word (a fruit name) is picked up from our collection and the player gets limited chances to win the game.
- When a letter in that word is guessed correctly, that letter position in the word is made visible. In this way, all letters of the word are to be guessed before all the chances are over.
- For convenience, we have given length of word + 2 chances. For example, word to be guessed is mango, then user gets 5 + 2 = 7 chances, as mango is a five-letter word.
Example
Python
import random
from collections import Counter
someWords =
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
flag = 0
try :
while (chances ! = 0 ) and flag = = 0 :
print ()
chances - = 1
try :
guess = str ( input ( 'Enter a letter to guess: ' ))
except :
print ( 'Enter only a letter!' )
continue
if not guess.isalpha():
print ( 'Enter only a LETTER' )
continue
else if len (guess) & gt
1 :
print ( 'Enter only a SINGLE letter' )
continue
else if guess in letterGuessed:
print ( 'You have already guessed that letter' )
continue
if guess in word:
k = word.count(guess)
for _ in range (k):
letterGuessed + = guess
for char in word:
if char in letterGuessed and (Counter(letterGuessed) ! = Counter(word)):
print (char, end = ' ' )
correct + = 1
else if (Counter(letterGuessed) = = Counter(word)):
print (& quot
The word is : & quot
, end = ' ' )
print (word)
flag = 1
print ( 'Congratulations, You won!' )
break
break
else :
print ( '_' , end = ' ' )
if chances & lt
= 0 and (Counter(letterGuessed) ! = Counter(word)):
print ()
print ( 'You lost! Try again..' )
print ( 'The word was {}' . format (word))
except KeyboardInterrupt:
print ()
print ( 'Bye! Try again.' )
exit()
|
Output:
Please run the program on your terminal.
omkarpathak@omkarpathak-Inspiron-3542:~/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: o
_ _ m o _
Enter a letter to guess: l
l _ m o _
Enter a letter to guess: e
l e m o _
Enter a letter to guess: n
l e m o n
Congratulations, You won!
Code Explanation:
- The code starts by importing the random module.
- This module provides a way to generate random numbers.
- Next, the code creates someWords, which is a list of fruit names.
- The list is split into spaces using the string ‘ ‘, and then each space is replaced with a letter.
- Next, the code randomly selects a secret word from our someWords list.
- This word will be used as the input for the game later on.
- 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).
- If not, then they are asked to enter only a letter.
- If they enter an alpha character, then it’s assumed that they want to guess at another letter in word .
- So, this part of the code checks to see if guess matches one of the letters in word .
- If it does, then chances is updated and flag is set to 1 .
- Otherwise, chances is decreased by 1 and flag remains at 0 .
- The next part of the code tries to guess at another letter in word .
- 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
- The code starts by importing the random module.
- This module provides us with a number of useful functions, one of which is the choice function.
- This function allows us to randomly choose a secret word from our list of words.
- Next, we create some variables which will be used throughout the program.
- These include someWords , word and letterGuessed .
- 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.
- 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.
- We then start looping through our list of words and randomly choosing a secret word from it.
Try it yourself Exercises:
- You can further enhance program by adding timer after every Guess
- You can also give hints from the beginning to make the task a bit easier for user
- You can also decrease the chance by one only if the player’s guess is WRONG. If the guess is right,
player’s chance is not reduced.
https://www.youtube.com/watch?v=Yt4I98jFIuU
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Aug, 2023
Like Article
Save Article