Lab 6: Word Game Due: 07:00:00AM on 2022-11-14


For this lab, you will be implementing a text-based word guessing similar to “hangman”. There are many variants of that game, so make sure to follow the specifications exactly. Read through the whole lab first before getting started. I provide given an overview of how to proceed and some hints near the end.

If you want, you may work with a partner on this lab. If you do, you must both be there whenever you’re working on the lab. Only one of you should submit the assignment, but make sure both your names are in the comment at the top of the file and you add your partner to your Gradescope submission (as described at the end of the assignment).

Getting Started

Download the starter file.

Playing the Game

When the program starts it reads a file containing a list of possible words. Download a text file with CS words (recall right click on the link and select “Save link as…” to download). Remember, this file needs to be in the same directory as your program.

After reading the file, the program randomly selects one of these words as the word that the user is trying to guess and then displays the following:

Guessed letters: 
Incorrect guesses left: *******
Word: _______

This display has three main components:

  1. Guessed letters: Shows the letters the player has guessed so far. The letters will be separated by a space and should be all capitalized. When the game starts, no letters have been guessed, so this area is blank.
  2. Incorrect guesses left: Shows the remaining number of wrong/incorrect guesses the player has before the game is over. Each remaining guess is represented as an ‘*’. In this version of the game, the player starts out with 7 incorrect guesses.
  3. Word: This shows the word that the player is trying to guess. Each underscore (_) represents a letter in the word. The word above is 7 letters long. As the user plays, the correctly guessed letters will be filled in.

The program runs by repeatedly prompting the user for the next letter. When the user enters a letter, there are three possible scenarios:

  1. If the letter has been previously guessed, then the program lets the user know with the message “Letter already guessed!” and prompts the user for another letter.
  2. If the letter has not been guessed before, but does not occur in the word to be guessed, then the number of incorrect guesses available is decremented by one and then the display is shown again, prompting the user for another letter.
  3. If the letter has not been guessed before and does occur in the word, then the underscored word is updated, with all occurrences of that letter in the appropriate place.

The following transcript shows all three of these different situations happening as the game is being played:

Guessed letters: 
Incorrect guesses left: *******
Word: ____

Guess a letter: e
Guessed letters: E 
Incorrect guesses left: *******
Word: _e__

Guess a letter: a
Guessed letters: A E 
Incorrect guesses left: *******
Word: _ea_

Guess a letter: r
Guessed letters: A R E 
Incorrect guesses left: ******
Word: _ea_

Guess a letter: s
Guessed letters: A S R E 
Incorrect guesses left: *****
Word: _ea_

Guess a letter: a
Letter already guessed!
Guess a letter: s
Letter already guessed!
Guess a letter: t
Guessed letters: A S R E T
Incorrect guesses left: ****
Word: _ea_

Guess a letter:

The game ends when either the user runs out of guesses, in which case the word is shown:

The word was: heap
Better luck next time!

or if the user fills in all of the underscores in the word, then the user wins and the number of incorrect guesses is printed out:

Guessed letters: A H S R E 
Incorrect guesses left: *****
Word: hea_

Guess a letter: p
You win!
The word was: heap
You guessed it with 2 incorrect guesses

Implementation Specifications

Besides following the game specification described above, your program MUST meet the following requirements:

Your program must contain at least the following four functions:

  1. read_words, which has a filename as a parameter and returns a list of the words in the file (you can assume the file contains one word per line).
  2. set_to_string, which takes a set as a parameter and returns the letters in the set as a string, with each letter capitalized and separated by a space.
  3. insert_letter, which has three string parameters: a letter, the current underscored word and the word the user is trying to guess. This function should return a new version of the underscored word with all occurrences of the letter filled in based on the occurrences of the letter in the word to be guessed. Please find an example below.

    >>> insert_letter("a", "__n_n_", "banana")
  4. play_wordgame, which has two parameters, the filename that contains the candidate words and the number of incorrect guesses the player gets. This function should then initiate the game and continue to run until the game finishes.

and meet these additional specifications:

Creativity Points

You may earn up to 2 creativity points on this assignment by adding improvements to your program. If you do, include in your comment at the top of your program what you added. Here are some ideas that you might consider:


One potential development approach

There are many different ways to implement your program, but here is one suggestion:

  1. Implement the read_words function and make sure that it correctly reads in the words in the file to a list. Don’t forget to remove the newline characters at the end of each line when reading in the words.
  2. Implement the set_to_string function. This should be very similar to the function you wrote for the prelab. Recall that sets are iterable, so you can iterate over the items in a set just like you can with a list.
  3. Implement the insert_letter function. There are many ways to implement this function, but one way is to build up the new string letter by letter (like we did for the encryption functions). For each letter in the new underscored word you’re creating, you either just copy what was in the underscored word before, or, if the letter appears in the word to be guessed at that position, you copy the correct letter.
  4. Implement play_wordgame incrementally, testing each step before moving on to the next:

    1. Read in the file, pick a random word and then display the underscored word. While you are developing, print out other information (like the word you’re supposed to be guessing) to help you debug your program. Just make sure to remove this information before you submit.
    2. Add the user input and add the letter that the user guessed into the underscored word appropriately (your functions from above should be useful).
    3. Add in functionality to keep track of the letters the user has guessed and to print out the letters guessed.
    4. Add the functionality to make sure that the user continues to be prompted to enter a new letter as long as the letter input has already been guessed.
    5. End the game when the user gets the right answer.
    6. Keep track of the number of incorrect guesses and also end the game when the user runs out of guesses.
    7. Print out the appropriate win or lose information.


When you’re done

Make sure that your program is properly commented:

In addition, make sure that you’ve used good coding style (including meaningful variable names, constants where relevant, vertical white space, etc.).

Submit your program via Gradescope. Your program program file must be named You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.

If you worked with a partner, only one person needs to submit to Gradescope, but that person does need to add their partner’s name as shown in Gradescope documentation. Make sure both names are included in the comment at the top of the file.


Features Points
Four functions implemented as specified 4
All words are read from the file 1
Each time a random word is picked from the words read 1
Initial underscored word shown correctly 1
Each guess is inserted appropriately in underscored word 2
Guessed letters correctly displayed 1
Guessed letters are updated appropriately 1
Guessed letters uses a set 1
Repeated requests to user on repeated letter 2
Incorrect guesses updated appropriately 2
Game ends correctly on win 1
Game ends correctly on lose 1
Handles lower and uppercase letters 1
Program starts automatically on run 1
Code design and style 5
Creativity points 2
Total 27

FAQ Excerpts Click entry title for more information

State in word game variables (and properties derived from that state)

Thinking about the example for insert_letter

>>> insert_letter("a", "__n_n_", "banana")

The current “underscored” word, what I would call the “working word”, is a kind of state. What properties of the game are embedded in this state? For example, can we tell if the user has won? How?

Creating a Text Figure for the Word Game

There are a number of ways to tackle this creativity option. I will show two potential approaches below for creating “hangman”-style figures using multi-line strings and the format method.