Create a word guessing game in Python
Tags: For Loop, Python, Artificial Intelligence, While Loop, Random

In my last post, I built a numeric guessing game in Python and the link for this post can be found here: Build a computer guessing game in Python

In this post I have made a guessing game, where the computer selects a word at random from a list of words, and the player has to guess the correct word by guessing which letters are actually in the word. The words that the computer selects at random are taken from the SOWPODS dictionary, which was compiled by Peter Norvig, and the link to the file can be found here: norvig.com/ngrams/sowpods.txt

The program is written around a series of while loops, for statements, and conditional statements that allow the player to keep guessing the letters contained in the correct word until he finally is able to guess the word. The rules of this problem can be found in this link: 31 Guess Letters.

The diagram below is a flowchart that I created to explain the mechanics of the game. I have written this flowchart on MS Word on my laptop computer and have pasted it to this post. The flowchart reads as follows:

At the beginning of the program, the libraries are imported, the SOWPODS text file is loaded into the program and converted to a list, and the variables are defined and initialised. The program selects a word at random, rand_word, from the list and another list, play_word, that will store the letters is created as well.

The two lists are input into a while loop, which will run until the two lists match each other. The player will be asked to guess the letter and the variable, guess_no, will be incremented.

If rand_word is not the same as play_word, the guessed letter will be put into a for loop, which will check to see if the guessed letter, play_letter, matches any of the letters in rand_word.

The two letters, play_letter and the letters in rand_word, will be put into an if statement. If the two letters match then play_letter will be inserted into play_word at the correct placement.

Once the player has correctly guessed all of the letters contained in rand_word, the while statement stops running, the player is congratulated, and a message is printed to say how many guesses it took to guess the word.

The screenshot below is the code for the beginning of the program where the libraries are imported, the SOWPODS file is read and converted to a list, and the variables are defined and then initialised:

The screenshot below is the code of the while statement that tests to see if play_word matches rand_word and the if statement, and for loop. Else statements are also included in the while statement and if statement if the inputs do not meet the criteria the program is asking for:

Below is the output of the game, which gives the player messages to help him to correctly guess the word. Once the player guesses the word, he is given a congratulatory message and told how many times he guessed before he got it right:

In summary, this was somewhat of a complex program to write because I had to convert the dataframe to a list before it would work. Python uses different functions for lists than it does for dataframes, so I had to utilise different techniques to be able to successfully write the program.