
Mad Libs is a word game where one player prompts others for a list of words to substitute for blanks in a story, before reading the – often comical or nonsensical – story aloud. In this assignment you will write a program to play Mad Libs.
Start with the story, in a text file whose name the player enters at the start of the program.

Humpty Dumpty
Humpty Dumpty sat on a [noun]
Humpty Dumpty had a great [season_of_the_year]
All the [government_official] ‘s [animal_(plural)]
and all the [government_official] ‘s [group_of_people]
Couldn’t [verb_meaning_create] Humpty Dumpty back together again.
The first line of the text file is the title of the story. Each additional line is a line in the story. Words to substitute are identified by the [square brackets] and the desired response type is shown in the square brackets, for example [noun]. If the word to substitute is a phrase, then individual words are joined with an underscore, as in [government_official] . (Don’t worry about the fact that the apostrophe-s combination is treated as a separate word.)
After entering the name of the Mad Lib text file (such as madlib.txt) , the program will ask if the player wants to see the Mad Lib with the answers from last time the program was run, or if the player wants to enter new answers.
If the player wants to enter new answers, the program will read the Mad Lib text file and will ask the question “Enter a ” followed by each word or phrase in brackets. If it’s a phrase, be sure to replace the underscores with spaces before showing the prompt. Your program should correctly display “a” or “an”, depending on whether the first letter of the prompt word or phrase is a vowel.
1
The program will
· Ask the player to enter the name of the Mad Lib text file
· Ask the player to enter y or n to enter new responses or use the most recent ones.
· Ask the player for responses, replacing underscore characters in bracketed words with spaces
· Replace each word or phrase in square brackets with the player’s response.
· Write the responses to a text file named (whatever the name of the Mad Lib file is) with _responses appended to the name, and then the .txt extension
· Count and print on the screen, the number of words added to the responses file
· Write the merged story to a text file named (whatever the name of the Mad Lib file is) with _merged appended to the name, and then the .txt extension.
· Write the merged story to the screen
· Convert the filename entered to lower case
· Convert the y/n response to lower case before checking
For example, if your Mad Lib file is madlib.txt, your program will create two text files:
· The file madlib_responses.txt will contain the responses
· The file madlib_merged.txt will contain the Mad Lib story with the responses replacing the prompts in brackets
Get this much done first.
Crazy Mad Libs
Here’s the crazy part. For each word in the madlib.txt file and for each response to a prompt the player types, that contains the second letter of the story’s title, reverse that word in the output. For example, if the title is Humpty Dumpty, and a word in the story or a response from the player is computer, you would output the word retupmoc (computer reversed) instead.
Add the “crazy” part to your program next. (Save a copy of the “Mad Libs” part first, just in case you need to revert to a working program.)
In a Box
Here’s the in a box part. Display the resulting Mad Lib in a box, that is surrounded by a line of asterisks
at the top and bottom, and an asterisk and space to the left before each line, and as many spaces as necessary and an asterisk at the right of each line. Center the title as the first line of the box, with an asterisk before and after. The width of the box is dependent upon the length of the longest line of the story after the prompts are replaced with responses.
Add the “in a box” part to your program next. (Save a copy of the “Crazy Mad Libs” part first, just in case you need to revert to a working program.)
2
The output of the program follows.
Welcome to Mad Libs
Enter the madlib file name as in madlib.txt: madlib.txt
Enter New Answers? (y/n)y
Enter a noun: computer
Enter a season of the year: summer
Enter a government official: President of the United States of America
Enter an animal (plural): aardvarks
Enter a government official: Prime Minister
Enter a group of people: swimmers
Enter a verb meaning create: assemble
7 lines written to madlib_responses.txt
******************************************************************
* Humpty Dumpty * * * * ytpmuH ytpmuD sat on a retupmoc * * ytpmuH ytpmuD had a great remmus *
* All the aciremA fo setatS detinU eht fo tnediserP ‘s aardvarks *
* and all the Prime Minister ‘s swimmers
*
* t’ndluoC assemble ytpmuH ytpmuD back together again.
*
*
*
*
*
****************************************************************** Output saved to madlib_merged.txt
Grading:
1. This assignment will be worth 6 % of your final course grade.
2. The input and output of your program need to appear in exactly the order that is shown in the sample interaction above.
3. A different Mad Lib story will be used when grading the program, so be sure your program follows these specifications.
4. Your program should compile without syntax errors to receive any credit. If a part of your program is working, you will receive partial credit, but only if the program compiles without syntax errors. As you program, be sure to save intermediate versions of the .py file each time you get a piece of the program running. This way you can always have something to submit that works on at least some of the requirements. Otherwise, what was working at one point may no longer work after further edits, and you may have no idea how to go back to the previous version (this is all too common!)
5. Name your script as madlib_shortname.py and upload it to Blackboard by the deadline.
3
Points:
This problem has several tasks. Complete as many as you can, or all of them for full credit.
Task
Points
Replace _ with space in [responses]
2
Replace [prompt] with response
3
Create merged and responses output file names
5
Count number of responses
5
Reverse words containing 2nd letter of title
10
Print resulting Mad Lib in a box
10
Correct output and user interaction
5
Code style, readability, comments, approach
5
45