HANGMAN — Python game

Rachit Saini
5 min readSep 21, 2023

Today, I will be coding a hangman game in python.

This is just to practise the basics of python programming. I will be making a basic outline of what I did to make this.

Lets begin coding

So assuming that you know that the basic of how the hangman program actually works. There are several areas that we need to tackle.

  1. Different words and guessing

If the words in the hangman game is always the same then it wont be fun anymore. Hence we will be making a list of words and we can keep on updating it in the future. This list would contain words that would be used to play the game.

2. Printing the instruction

After choosing a random word we want to print blanks or underscores to represent characters of the word. For doing this we will be know the number of character using the length function and then go ahead and print the underscores.

3. Guessed word. Making it work to see if it is correct or not

For this I will take an input of the guessed word and run a for loop for each character of the chosen word.

For each itteration of the loop I will then insert an if statement to print “right”, if gussed character is equivalent to any character of the word or else it will print wrong. Se the image below, it will make things clear.

4. Replacing the correct guess

Well now that we can compare the words, now I want to replace words if I am making progress.

Well as you see we are using a for loop for comparing the chosen word and guess char, we can instead use the range itself there as we already know the number of letter. No instead of printing printing underscores we can instead make a list of underscores equavelant to the number of characters of the chosen word. After doing this we can have a conditional to chance the specific underscore in the display list when the guess is right.

5. Now making the game iterative.

Now that we have the games basic function working lets repeat the process until the person is able to finish the whole word.

For this I will be using a while loop. I will run it until the chosen word remains not equal to the word formed using the underscored. If it becomes equal then the game is over and all the underscores have been replaced with the right guess.

So I made a empty string variable named word_formed and made it to empty at the start of the while loop. So every time it runs I can start fresh. Now it take in the guess character and compares to every character in the chosen word. If any of the character is right we will add it to the display list and form a word. Here is the code.

6. Adding lives

Now that we have the basic hangman working we need to add a score. In our current version we can play until we get the word right but in actual hangman we can only make a limited amount of mistakes. Lets add lives to the game.

For this, I added a variable named lives and made it equal to 8. Now I changed the while to loop and added a if conditional which reduces lives if the guess was not in the word.

7. Adding bling to the game

Now that our game is functioning, we will now be focusing on making it better. Few things that we need to start with is clearing the screen at start of each loop in the while loop. For that we will import os and a run system level clear.

Next we will display you loose or win according to the lives left after while loop ends.

Now use this link (click here) to get the ASCII version of the hangman logo.

And use think link (click here) to create the hangman according to the levels.

I have saved the ASCII hangman character in a list and display from the list according to the lives.

8. Now that the game is functioning and the thing are working alright, we need to expand the word list. But one thing I noticed is that if the word is a two letter word then the space is also represented as a underscore. Lets solve this. And then add more words.

To do this I am making a few changes.

First I added an if else conditional to add the extra blank spaces to the formed word but kept a single space in the display array. I did this so that formed word would show extra spaces but display array would only have a single space making it easy to compare words. Second I changed the condition in while loop instead of comparing it with formed word, I instead compared it with a string formed from combining the display array.

Now that the game is functioning properly, I will add more words.

If you wish to play the game. CLICK HERE

Thanks for seeing my post!!!

--

--