Hangman Game | Python

PLAY THE GAME HERE

My next project is a hangman game! This program generates a random word selected from a predefined list and then allows the player to make guesses one letter at a time either updating the blank with a correct letter or drawing a part of the hangman for an incorrect letter. This project allowed me to build a further understanding of how to utilize ‘while’ loops as well as ‘if’ and ‘for’ statements. Let’s take a look at how the code works further…

First, I import a few modules titled ‘random’, ‘hangman_ASCII’, and ‘words’. The ‘random’ module allows the program to select a word from the list in the ‘words’ module; while ‘hangman_ASCII’ holds the ASCII art used. I also import ‘system’ from the ‘os’ module, enabling me to define a function titled clear() which clears the terminal after each play (more on this later).

I then declare three variables for use in the program. First, I set ‘end_of_game’ to False. I will use this later to pull the game out of the ‘while’ loop which it’s contained within. I also set ‘user_life_counter’ to a value of 6 and declare an empty list titled ‘guess_bank’ which is where I will store the player’s previously guessed letters.

Lastly, I define the clear() function which checks if the name is ‘nt(for Windows) and if so, clears the terminal via the ‘cls’ system command.

Next, the program prints both the main hangman logo and the first hangman stage, which is the blank ASCII gallow. I then use the ‘random.choice’ module to assign the secret word to a variable called ‘chosen_word’.

A blank list titled ‘display’ is declared and for each letter in the chosen word, a blank (underscore) is added to the list. The full list with an underscore for each letter is then printed below the blank ASCII gallow.

All the remaining code is then held within a while loop which checks that ‘end_of_game’ is still False after each turn.

A variable titled ‘guess’ is then created and stores the current guess of the player. Immediately after the guess is input into the terminal, the clear() function is called to effectively refresh the screen. By not calling this function, each turn would list directly below the previous, causing a poor user experience.

After the clear() function is called, the program prints the hangman logo once more before checking if the guess is stored in ‘guess_bank’ and if so, prints ‘You have already guessed’ with the letter entered.

The program then checks if the guessed letter is within the chosen word. If so, the underscore in that letter’s position is replaced with the guessed letter. After iterating through the entire word, the ASCII gallow is printed and the guessed letter is added to the ‘guess_bank’ list for reference.

If the guess is not within the chosen word, the program first deducts 1 from the ‘user_life_counter’ and prints the ASCII gallow that corresponds to the player’s current ‘user_life_counter’ value. A print statement is then displayed with feedback letting the player know that the guessed letter is not in the word. Next, the guessed letter is then added to ‘guess_bank’ for reference.

The program then checks if ‘user_life_counter’ is holding a value of 0. If so, a print statement of the ‘ASCII.lose logo is displayed and the break keyword is triggered to pull the program out of the while loop.

Finally, if ‘user_life_counter’ is not holding a 0 value, the program checks whether there are any remaining underscores held in ‘display’. If not, ‘end_of_game’ is set to True which pulls the program out of the loop and prints the ‘ASCII.winlogo. The condition of no underscores in ‘display’ can only be met if the player has guessed all spots correctly without ‘user_life_counter’ holding a value of 0, which is why it’s used as the winning factor.


Organizing the ‘if’ statements and ‘for’ loops in the proper order was a bit challenging at first. But, I am finding debugging a bit easier, specifically as I begin learning how to best use the built-in debugging tools of VSCode.

You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!

Previous
Previous

Caesar Cipher | Python

Next
Next

Password Generator | Python