“Hangman with Image Display” — Series 13: #LearnPythonThroughProjects

Aminah Mardiyyah Rufai
5 min readOct 7, 2020

In the previous series, we built a simple version for the ‘Hangman game in Python”. In this series, we are advancing forward, improving on the previous program, and testing our skills.

As mentioned in the previous series(view here), the HANGMAN GAME is all about solving a puzzle and guessing a word or phrase correctly before the ‘hanging man dies’. An image is usually displayed, where a hanging man slowly begins to appear, bit-by-bit, for every wrongly guessed word, and a life/chance or try in the game is lost.

Hangman Image Display

In this series, we will be implementing the image display logic, using functions and data structures skills, as well as fix some minor bugs in the program as well.

We will also be making use of modules in python, some new built-in Python Modules. New concepts that will be encountered as a brief introduction today are:

  • File handling in Python
  • Fetching/Getting data/information using Python

Let’s get into in!

HANGMAN GAME WITH IMAGE DISPLAY IN PYTHON

  • Open a new python file. Save it as ‘hangman-new.py
  • Next, using the same hangman program from the previous series, make the following changes:
  1. Import the Built-in Python Modules: “

i- json module

ii- urllib module

iii- random module

2. Add the following variables to randomly generate a word from a defined link( a Github repository)

3. In the ‘play_again()’ function, update the code as follows:

3. Update the ‘get_word()’ function.

4. Finally, update the main ‘game_run’ function as follows. Take note of the lines highlighted for the changes. See the images below:

Notice that in the image immediately above, the prompt for displaying the hangman image was initiated. Let’s define the function controlling that below:

  • Define a new function for the hangman image.
  • Use the list Data structure and intuitively draw the hangman image.
  • Use indexing and multiple line string formatting to draw image progression at each position such that the images will be displayed accordingly for a given number of tries left.

See images below:

The program is complete!!

Save and run the program.

Do not forget to call the main function ‘game_run’to get the program running.

PROGRAM RUN

Isn't this so Awe-Inspiring!!

Another cool project aced.

The Flow of the program is just as was explained in the previous series, the few additions here are:

  1. Adding an automatic random word generator using the concept of fetching data from the web. This saves us the stress of creating a data structure for the words to be generated.
  2. Using the concept of indexing in data structures to create a function that simultaneously displays the stages of the hangman image for every missed try or wrongly guessed word.

For file handling and fetching data, we will explore these further in future series. The aim here was to get exposed to more built-in Python modules and a peek into how a simple word generator can be created in Python.

Thank you for reading! Hope you had as much fun building this. Do look up the resources below, and find the link to the Github repository for the code used below as well.

See you in the next series!

CONNECT ON SOCIAL MEDIA

LinkedIn: Https://www.linkedin.com/in/aminah-mardiyyah-rufa-i/

Twitter: @diyyah92

--

--

Aminah Mardiyyah Rufai

Machine Learning Researcher | Machine Intelligence student at African Institute for Mathematical Sciences and Machine Intelligence | PHD Candidate