Hangman is a very interesting game. It must have been played by almost everyone at least once in life. The back-benchers and movie lovers will agree to me. It is a guessing game where the players select the domain of words to be guessed. This post is about creating Hangman Python Code.
How it is played?
First player challenges second player to guess the name or word he has chosen. The game begins by presenting the number of letters in the word to be guessed. It is played by writing blank spaces equal to the count of characters in the word.
Second player who has to guess says a letter from alphabets a-z. If this character is in the word, the first person fills it in the correct places in the set of blanks written by him.
If the spoken letter is not in the word to be guessed, one letter from “HANGMAN” is struck off. The chances are reduced by one. It means first wrongly mentioned letter reduces chances of guessing from 7 (characters in word Hangman) to 6.
This process of guessing one letter, filling or striking off one character from HANGMAN is continued.
It stops either when the word is guessed or when 7 chances of guessing the letters to complete the word is over.
Creating Hangman Game in Python
To create this program the problem is divided into three parts.
- Finding the locations of the guessed letter in the word to be guessed- The function findloc(alpha, word) finds the locations of parameter alpha in parameter word and stores in the global array loc.
- Replacing the letter in these locations- The function display(alpha) accepts the alphabet entered by the player and displays the partially completed string of guessed letters and blank space
- Decrementing the count of chances remaining-If the letter is not part of word, the chances are decremented by one and one starting letter from HANGMAN is removed. This is done in the main program within while loop.
The code is executed only once. If you want to play the game again, you have to re-execute program . Modify the code with a Python loop to run multiple times asking the user to continue or exit.
import random import sys def findloc(alph,word): i=0 k=0 while (i<len(word)): if (alph==word[i]): loc.append(i) k+=1 i+=1 def display(alph): i=0 temp= if (loc!=None): while (i<len(loc)): check[loc[i]]=alph i+=1 i=0 while (i<len(word)): if check[i]!=None: sys.stdout.write(check[i]) temp.append(check[i]) else: sys.stdout.write(" _ ") temp.append("_") i+=1 return "".join(temp) # an array of movie names from where one movie name is picked randomly lib=["casablanca","day and knight", "jupiter","interstellar","gravity","moonlight", "inception","waterhorse","devil wears prada", "bedazzled","enchanted","cinerella"] loc= hm="hangman" fin="" #picking random movie name word=lib[random.randrange(len(lib))] #getting its word count check=[None]* len(word) # initializing chances chances=0 print("Guess a hollywood movie with ",len(word)," letters!!!") print("") print("let's begin"); while (chances<7): print("") #printing remaining chances by removing letters from word hangman when an entered character is not in movie name to be guessed print(hm[chances:]); lett=input("enter character--->") #finding locations of entered letter findloc(lett,word) #displaying partially completed movie name after filling the entered letter fin=display(lett) #incrementing chances availed when a enterted letter is not part of moviename if len(loc)==0: chances+=1 loc= #comparing partial and final word if (word==fin or chances==7 ): break print("") print("") if (word==fin) : print("Congrats!!You are a movie buff:)") else: print("HANGMAN :(") print("The movie was ",word)