Learn By Doing: Python Hangman Game

Teacher Turned Techie
5 min readApr 2, 2022

In my quest to become a better programmer, I am diving deeper into the programming languages during my “code every day of 2022” personal challenge. I’ve watched countless hours of tutorials and decided it was time for me to build something from start to finish. One of the best ways to learn is by doing, and one of the best ways to become a better developer is writing code. This article aims to explain the key concepts found in the Hangman Game I’ve built using the Python programming language. You can find and download the files for my hangman game in this GitHub repository.

Overview

It’s one thing to watch a tutorial and copy what the instructor does, and it’s another to break down every line of code and understand what is being used and why. In the key concepts below, you will find what was used in this python program that helped me build a successful hangman game.

Key Concepts

  • modules
  • import
  • random
  • .upper( )
  • string
  • ascii_uppercase
  • sets
  • .add( )
  • __main__

Modules

Modules are Python files (with the “.py” extension) that contain code that can be imported into another Python program. Modules can be looked at like a library or a set of functions you want to use in your program. Modules allow developers to organize functions, classes, variables, and code blocks in the same file. When writing bigger programs, breaking down large code blocks into smaller modules is the best practice.

The modules used in the hangman game are wordList.py and hangmanVisual.py. The wordList.py module has a single array that includes a list of over 800 words that the computer will use to choose a word for the hangman game. The hangmanVisual.py module has the “visual imagery” that represents the hangman game each time the user makes a guess.

Import

The Import System in python allows one module to gain access to another module by importing the data from the second module into the first.

imports for hangman game

The comments tell you what each import does in the hangman.py file.

Random Module

The random module uses a pseudo-random generator to generate a data point randomly. It can be used to generate random numbers, print a random value from a list or string, or be paired with different methods to increase its range of use.

In the hangman.py file, there is a getWord function. A randomly chosen word from the words list in the wordsList module will be stored in the word variable.

.upper( )

The .upper() method converts all characters in the string it manipulates from lowercase to uppercase. In the graphic above, the .upper( ) method manipulates the word variable. Remember, the information stored in the word variable is chosen from a list of strings in the wordList.py file. This method changes any lowercase character in the word variable to an uppercase character.

String Module

The string module contains functions to process Python strings. In addition to manipulating strings, you can use the string module to convert strings into other data types.

acii_uppercase

In Python3, acii_uppercase is a pre-initialized string that will give you all 26 letters of the alphabet, but uppercased. In the graphic below, string.ascii_uppercase is stored in a set in the variable alphabet. This is where the program will go to manipulate the letters a user guesses while playing the game.

Sets

In Python3, sets are one of the four data types (others including list, tuple, and dictionary) used to store multiple items in one variable. Items saved in sets are unchangeable and unindexed; however, you can add new items and remove current items. You have to use curly brackets “{}” to create the set. Sets are also unordered, so you have no choice in which order the items in the set will appear.

In the graphic above, three sets are being created. The first set stored in the variable wordLetters stores the letters used to create the word that the user will be guessing during the Python game. The second set stores all alphabet letters in the variable alphabet but uppercased. The third set stores all of the letters that the user has already guessed.

.add( )

In Python3, the .add( ) method adds a given element to a set.

In the graphic above, the .add( ) method adds the letter that the user has guessed during the game to the set stored in usedLetters.

__main__

In Python, __name__ == “__main__,” the Python interpreter reads the source file and defines special and global variables before executing code. If the interpreter is running the source file as the main program, it sets the variable __name__ to have the value of “__main__.”

Conclusion

I have come a long way since I have quit my pursuit of becoming a programmer. The only way you can get better at writing code is by writing code. Start small and manageable. There are several beginner-friendly Python programs you can try to recreate. However, don’t just blindly copy code. Make sure you have a firm grasp of what is used and why. Reference the official Python documentation, W3Schools, or any other free resource if you ever get stuck. Remember, friends, do or do not; there is no try. Happy learning!

--

--

Teacher Turned Techie

👩🏾‍💻 I break down complex concepts so you don’t have to.