COMP 118 – Lab 4: President’s Word Game

Project Goal

The goal of this program is to create a game where the player guesses the more common word in President John F. Kennedy’s Inaugural address. The project includes the speech in the file “Kennedy1961.txt”. The program will show two words from the speech, and the user will guess which one is more common. Score is kept of their success or failure. Below are all (or almost all) inaugural addresses. You are to pick which one you wish to use.

In order to make the game more interesting, we want to rule out common words like “the”, “to”, “and”, etc. We call these “stopwords”, which means we ignore them. They are included in the file “StopWords.txt”. The program should ignore these words.

All the inaugural addresses

Learning Goals

  1. Reading from files
  2. Parsing lines into words
  3. Searching a list for a matching string
  4. Using a random number
  5. Using functions (including parameters and returning values)
  6. Checking for input errors

Example

Below is a sample game play:

In this game you choose the more popular word
in President Kennedy's 1961 presidential
inauguration speech.
The words are: 1. summons 2. go
Enter number of more common word, or 0 if the same. 2
summons occurs 1 times
go occurs 3 times
You got it right!
Score 1/1
Go again? ('y' or 'n'):y
The words are: 1. war 2. cost
Enter number of more common word, or 0 if the same. 1
war occurs 2 times
cost occurs 1 times
You got it right!
Score 2/2
Go again? ('y' or 'n'):y
The words are: 1. hard 2. loyalty
Enter number of more common word, or 0 if the same. 1
hard occurs 1 times
loyalty occurs 1 times
Better luck next time!
Score 2/3
Go again? ('y' or 'n'):n

Method

You are to write a program to play this game. This program will be broken down into several function, which will be defined for you. You are to write and test each funcition individually, and then assemble the functions in the final solution.

Steps

Please complete the definitions of each of the following functions. For 1-5 you are implementing and testing specific functions for the solution (in some cases using previous functions). Then assemble the functions into a complete solution in 6. Submit the complete solution.

  1. Write and test function loadStopList(): Link
    This reads a file of stopwords, and loads them into a list.
  2. Write and test function isStopWord(aWord,stopWords): Link
    This function is passed a word and a list of words. It returns true if the word is in the list of words.
  3. Write and test function loadWords(filename,stopList): Link
    This function is passed a filename and a stopword list.
    It opens the file and reads each the words, removing punctuation and converting it to lower case.
    If the word is not in the stop list (use the isStopWord function) add it to a list of words.
    Return the list of words.
  4. Write and test function countWord(wordList,aWord): Link
    Given a word aWord, count how many times the word is in the word list wordlist.
    Return the count
  5. Write and test function pickRandomWord(wordList): Link
    Use the random library to return a random word from wordlist.
  6. Complete the final program by assembling all of the functions written above into a single program file.

Turn in

Submit the 5 smaller programs and the completed program saved links to Moodle. Be sure to include the standard information and the honor statement at the top of your program file. Any submission that is missing the Academic Integrity Statement will not be graded.

Grading

RequirementsGrading CommentsPointsScore
Fully functioning code that works exactly like the game shown above50
Completion of all the steps described in the project statement40
Fully commented code, with your name, date, description, honor statement, and comments on all functions.10
Total100

Scroll to Top