CSCI 120 Fall 2018 ProjectIn this project you will be creating a Mad Libs game. If you don’t know what Mad Libs are, see
the Wikipedia page on them . This program will have several pieces and it is strongly
recommended that you break it down into smaller pieces and work on completing a few parts
every week until the assignment is due. You will not be able to complete this entire project the
day it is due.
Another part of the project is creating/providing your own Mad Libs. I have included a single
example and you will need to create two additional Mad Libs that you commit with the project.
Mad Lib Text File Format
The Mad Lib games are given in text files. One sample Mad Lib game is provided in
madlib1.txt . Eventually you will need to create two additional games in the files
madlib2.txt and madlib3.txt . However you can work on much of the program before you
work on those games.
If you open up the example madlib1.txt you will see normal words along with blanks that
need to be filled in. The blanks are words that will have to be typed in by the user to complete
the story. The blanks always start with __ (two underscores), followed by a description of the
word needed (with spaces replaced by _ (single underscores)), followed by __ (two more
underscores), and then optionally followed by punctuation (for example . , ? ! : ).
For example, on the first line of the example:
Dear __name_of_a_relative__,
where the __name_of_a_relative__, needs to be replaced by a name of a relative given
by the user (and the punctuation , will be kept in the final output).
Many times the description of the word will be a simple part-of-speech such as noun or
adjective, however sometimes a more complex word is needed such as a body part or a verb
ending in -ing. For even more examples see the example text file.
The Mad Lib games you create must include at least 10 blanks each and should have a wide
variety of word types to be filled in. They should entertain me while I am grading your
assignments.
1
CSCI 120 Fall 2018
The Program
Your program will randomly choose one of the 3 madlib#.txt files and open it. This file will
then be read line-by-line and each line will be read word-by-word. If a word does not begin with
__ (two underscores) then it is concatenated onto a string that contains all of the words so far in
the current line (with spaces between them). When done processing a line, the entire new line is
appended to a list. Once all lines have been read in the file, go through the entire list outputting
each line.
If a word is found that starts with __ (two underscores) then more work is required before
concatenating the word to the new line. First, the last _ must be found (can use the rfind
string method for this which works just like find but starts at the end of the string). Everything
after the last _ must be removed and saved for later (it is the punctuation, there may be nothing
as well). At this point your word starts and ends with two underscores at each end. Remove
them. Then replace all of the _ in the middle of the string with spaces (this can be done with a
loop, splitting and joining, or the replace string method). At this point we have the description
of the word that we need to ask for from the user. Prompt the user to enter that type of word and
whatever they type in will be the new word. Finally, add back the saved punctuation and
proceed with adding the word to the new line of text being created as normal.
See the last page for an example run of the program with the provided game file.
Approach
There is a lot going on in this program. Do not try to do it all at once or even in the order I
described it. It will likely be easier to work on the program through the following steps. After
completing a step make sure to fully test the program to make sure that it is working so that you
can move forward and know that there are not any problems with the previous code.
1. Create a program that opens madlib1.txt , reads each line, goes through each word, and
prints out each word (don’t even check for blanks).
2. Adjust it so that instead of printing each word it adds each word to string variable for the line
being created and prints out each line all at once after going through each word on a line.
3. Adjust it so that instead of printing each line immediately it saves the lines in a list and after
going through all lines goes through the list and prints out all of the lines.
4. Adjust it so that if a word is found containing a blank (starts with two underscores) it gets
input using the prompt “ Enter a(n) : ” where is the entire word
including underscores and punctuation. Store the user’s input back in the variable you are
using for the word so it will be added to the entire of the line being created.
At this point you have a mostly functional program! It isn’t all that nice to
use and doesn’t handle punctuation, but it works and replaces words!
2
CSCI 120 Fall 2018
5. Adjust it so that it removes the first two and last two characters of the blank description
(don’t worry about punctuation now). Should look a little nicer now.
6. Adjust it so that it replaces all remaining _ with spaces. It should be even better looking now.
7. Adjust it so that before it removes the first/last two characters it finds the last underscore and
then saves and removes everything after the final underscore. This saved value is added
onto the word after you get the new word from the user.
At this point you have a functional program! Everything should be
working. The one thing missing is the two additional Mad Lib games and
your program picking one randomly to run.
8. Create the two additional Mad Lib files and test them each individually by explicitly opening
them instead of the first one. Make sure they all work as expected.
9. Adjust the program so that instead of opening a fixed Mad Lib game it randomly picks a
number from 1 to 3 (inclusive) and opens that Mad Lib game.
Grading
This project will be graded out of 100 points as follows. Additionally, the first 2 Fridays you must
show sufficient progress which is approximately completing 3 steps from the above list.
Insufficient progress eill deduct up to 10% per week.
10 2 Additional Mad Lib game files provided, each with 10 or more blanks and with a variety
of word types in those blanks. You do not have to come up with your own, but if you use
them from elsewhere make sure to include an attribution to the source. (Part 8)
10 Randomly picks one of the three game files to load and opens it. (Part 9)
10 Loops over each line of the open file and each word within each line. (Part 1)
15 Adds each word to a growing string after processing. Adds each new line to a list and at
the end goes through that list printing out each line. (Parts 2-3)
15 When blanks are found the user is prompted and the result is stored. (Part 4)
15 The prompt does not display any trailing punctuation from the blank or any of the
underscores. (Parts 5-6)
15 The punctuation after blanks is removed for prompting and re-added after the user has
entered the new word. (Part 7)
10 Good style: descriptive variable names, comments, non-redundant code, correct usage
of language features, etc
Example output on next page.
3
CSCI 120 Fall 2018
Welcome to Mad-Libs!
Enter a(n) name of a relative: Uncle Tom
Enter a(n) adjective: fun
Enter a(n) adjective: creative
Enter a(n) adjective: green
Enter a(n) celebrity's name: Alan Turing
Enter a(n) adjective: quiet
Enter a(n) name of person in the room: Angela
Enter a(n) adjective: swollen
Enter a(n) verb ending in ed: rented
Enter a(n) body part: funny bone
Enter a(n) verb ending in ing: climbing
Enter a(n) noun (plural): apples
Enter a(n) noun: tape
Enter a(n) adverb: daily
Enter a(n) verb: ski
Enter a(n) name of a relative: brother
Enter a(n) your name: Jeff
Dear Uncle Tom,
I am having a(n) fun time at camp. The counselor is creative and the food
is
green. I met Alan Turing and they are as quiet as you would imagine.
Unfortunately, Angela is swollen and I rented my
funny bone so we couldn't go climbing like everybody else. I need more
apples and a tape sharpener, so please daily more when you ski back.
Your brother,
Jeff