Project 5: Mad Libs Improved

Exercises

Turn in exercises 15, 16 and 17 with this project.

A Flexible Madlib Generator

Last week's Mad Lib generator only worked with a single form of Mad Lib. This week you will read an input file that contains a template for a Mad Lib. Then you will prompt the user for three words and complete the Mad Lib. Your program will be given an input file like this one:

madlib_sample1.txt
{}! he said {} as he jumped into his convertible and drove off with his {} wife.
exclamation
adverb
adjective

The file will always have exactly four lines.

  1. A Mad Lib with three embedded {} marks.
  2. The type of word used to fill the first blank
  3. The type of word used to fill the second blank
  4. The type of word used to fill the third blank

You project will be executed from the command line with a single argument, the name of the Mad Lib text file. Here's an example using the file above:

$ python3.6 project5.py madlib_sample1.txt
Please enter an exclamation
Please enter an adverb
Please enter an adjective
Eureka! he said bigly as he jumped into his convertable and drove off with his purple wife.

After you've printed the Mad Lib your code should save the completed Mad Lib in a file. The file should be named after the name of the input file with a .complete suffix. So, after running your program in the example above, there should appear the file below:

madlib_sample1.txt.complete
Eureka! he said bigly as he jumped into his convertable and drove off with his purple wife.

Your code should work with different Mad Libs. Here are a couple more test cases:

madlib_sample2.txt
The {} fox {} over the lazy {}.
adjective
verb
noun
madlib_sample3.txt
Why did the {} {} the {}?
noun
verb
noun

Running your Mad Lib generator on the samples should produce the files:

  • madlib_sample1.txt.complete
  • madlib_sample2.txt.complete
  • madlib_sample3.txt.complete

Requirements

  1. All files must have a docstring with the class and your name.
  2. Submit your program in a file called project5.py
    1. Your program must take one argument (the name of a file)
    2. Your program must create a file (or overwrite an existing file) named after the input file (see above)
    3. You can assume the file will always be correct (you don't have to handle errors)
    4. You must print the completed Mad Lib on a line by itself.
    5. You must close all files before your program exits.

Web Based Madlibs (Optional)

The advanced project for this week is a web-based version of the improved Mad Lib generator. This week's lecture notes Reading and Writing Files contains starter code at the bottom of the page. Upload the madlib.py file into your Cloud9 environment.

The example code works but takes only a single word Madlib. Expand the code to take three word madlibs. There are three pages in the application and two forms. Pay special attention to how data is transferred from one form to another in hidden fields.

  1. The user loads the starting page (GET: /)
  2. The madlib form is submitted to the same page (POST: /)
  3. The words form is submitted to the result page (POST: /result)

When you're running your web-based program view the page source of /words. You can view the page source on most browsers by right-clicking the page and selecting “View Source” from the context menu. Notice that the madlib is in the source. Do you understand why it has to be there?

The advanced project should log all Mad Libs in a file called madlib_log.txt. Every time you display a completed madlib append it to a new line to the file. Hint: open the file for appending.

Requirements

  1. All source files should have docstrings
  2. Submit your code in the file project5_adv.py
  3. Your madlib program should take templates with three embedded {} markers
  4. Your madlib program should take three input words.
  5. Your madlib program should log all madlibs to the file madlib_log.txt

Grading

  • 5 points for exercises
  • 5 points for a correct submission
  • 10 points for the correctness of your program