Hello coders!! In this article, we will be learning about the game hangman and its coding on python. At first, we will understand the game rules and learn the step-by-step implementation of the game in Python.
What is Hangman?
It is a pen and paper game based on guessing, played between two or more players. The number of guesses is limited. One person picks a word, and the other players try to guess that letter by letter, within the limited number of guesses. If they successfully spell out the word in the given number of guesses, they win, or else they lose.
Illustration of the Hangman Game in Python:
Let the word be: FLOWER
Let us start the game!
The game is now over and the player has won!
Implementation of Hangman in Python:
import time from time import sleep name = input("Enter Your Name:") print( "Hello" + name) print("Get ready!!") print ("") time.sleep(1) print ("Let us play Hangman!!") time.sleep(0.5) word = "Flower" wrd = '' chance = 10 while chance > 0: failed = 0 for char in word: if char in wrd: print (char) else: print( "_") failed += 1 if failed == 0: print( "You Won!!Congratulations!!" ) break guess = input("Guess a Letter:") wrd = wrd+guess if guess not in word: chance -= 1 print ("Wrong Guess! Try Again") print ("You have", + chance, 'more turn' ) if chance == 0: print ("You Lose! Better Luck Next Time" )
We have imported the module time to use the sleep() function. This function helps in creating a pause before the execution of the next statements.
Let us discuss about the different variables used:
- name: to store the name of the player
- word: the word that the player needs to guess. In this case, we have used the word ‘Flower.’
- chance: signifies the number of guesses available for the player
- failed: a counter variable to keep track of wrong guesses
- wrd: a variable to store the letters that are correctly guessed
- guess: variable to store each letter entered by the player
Let us Understand the Working of the Hangman Code in Python:
- At first, we take the name of the player as input and welcome him to the game.
- For this code, the word is set to ‘FLOWER.’ We iterate the program till the number of available turns is greater than 0.
- We then checked if the character in the initial word is also available in the word variable.
- If yes, then we print it, or else we print a blank in its place. We also increase the failed counter for every wrong guess.
- After every turn, we reduce the chance variable by 1 to indicate the number of chances remaining.
- If the value of chance becomes zero, and the person has yet not spelled out the word correctly, he loses.
- If the person spells out the word correctly before the value of chance becomes zero, he wins.
More to Read >> Rock Paper Scissors in Python
With this, we come to the end of the article. This was an easy approach to make the game of hangman in python. It can definitely be enhanced by using the Tkinter library or allowing more words as per the coder’s desire. The purpose of this article was to provide a simplistic approach to develop the game of hangman in python.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.