A file will contain a list of candidate words, and one will be chosen at random.

computer science


A file will contain a list of candidate words, and one will be chosen at random.   The user is then prompted for letters to guess.  Correct guesses will be placed in the corresponding blanks; there is a limit to how many incorrect guesses are allowed.

An input file example might look something like this:

phrase,my hovercraft is full of eels

Here is a sample interface:

What file will the words come from?   hangman
This one is looking for a phrase:
______ ____ __ ___ ______ ____ __ ____
What is your guess?  e
There are 3 e's
______ ____ __ __e ______ ___e __ ___e
What is your guess?  a
There are 2 a's
A__a__ ____ __ __e ______ ___e __ ___e
What is your guess?  s
There are 2 s's
A__a_s ____ __ __e ______ s__e __ ___e
What is your guess?  i
There are 3 i's
A__a_s ____ __ __e __i___ si_e __ _i_e
What is your guess?  z
There are no z's (1 incorrect guess)
What is your guess?
There are 4 o's
A__a_s _oo_ o_ __e __i___ si_e o_ _i_e

It would be very appropriate to have one execution of the program to allow a user to try to guess at more than one entry in the file.

Using Ascii art to sketch out the hanging man would be an interesting feature, but the more challenging part of the program is the game itself.


It is expected that there will be at least two distinct functions defined for this project.   Each function's purpose should be summarizable in one or two sentences.   a function should be fewer than 30 lines of code (or not much more than that), not counting documentation and blank lines.

Each function should have a meaningful name and meaningful parameters.   Each should have a docstring (using triple quotes) at minimum describing what the function does.   The complete interface should be clear from the function heading and its documentation.

The function certainly may define additional local variables to complete its task.  The parameter list should only contain values and variables that must be shared with its caller, and no other hidden communications (such as no global variables).

If your submitted project design seems insufficient to these expectations, you are certainly permitted (and encouraged) to improve upon it, since that will help you to complete the program on time.


Use of Data Files

The data files are intended to allow a great deal more flexibility in the application areas without having to modify the program code.   Each program is expected to get all of the content data from the data files.   The content is not to be hard-coded into the Python code itself.

data file consists of the possible words to be guessed at 
The program should prompt the user for the name of the data file.   Whether you require the user to type an extension (such as ".txt") or whether the program automatically adds the extension is up to you.   You may assume that the user running the program knows the name of an existing data file in that same folder.

If you wish to remove that assumption, and give a user a list of choices of available files, you may do so, for 10% Extra Credit.  This would require getting a list of files from the Operating System (consider the 'listdir' method in the 'os' module).  

Your program should not make any particular assumptions about the exact size of the data file.  It should just be able to read to the end of the file (the for loop can do that).  A recent recitation demonstrated that it is rather easy to identify how many file lines were actually found, and the program can then make use of that number.

Special Hints

Here are a few things that would make the assigned projects a little interesting, with some hints about how to go about them.


The Extra Credit portion to the posted solution to Homework 5 built up a solution string one letter at a time, much in the same way as Hangman does.  This modified answer is most easily done with a list, replacing letters into each position where it belongs, with the string join method allowing the answer to show up in a clean fashion.

Your program should take care of case-sensitivity, not treating 'a' and 'A' as separate letters.  Whether your answers are all upper case or lower case is up to you -- just make sure that the user at the keyboard gets the appropriate responses.


Submission Details

At least three files will be submitted instead of just one.

Include at least these three items in your submission on Canvas:

  • The Python file (.py) containing your program solution
  • A data file of at least 10 lines (may be more), which would be representative of a typical input to your program
  • A 'small' data file (perhaps fewer than 6 lines) that would also be acceptable to your program

There are a couple reasons for requesting multiple data files:

  • It demonstrates the ability to read the data from any named file
  • It demonstrates that the program does not rely too much on hardcoded data
  • It shows that the program can flexibly react to files of different sizes
  • It allows the grader to run your program very quickly, using the small file, to improve grading time

Your program should, of course, not be aware of the exact content of the files you submit.   The graders should be free to make any change to the data file that might help them evaluate your program.



Related Questions in computer science category