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 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
 ... User types: "Eureka"
Please enter an adverb
 ... User types: "bigly"
Please enter an adjective
 ... User types: "purple" 
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.

Improved Web-Based Mad Libs

The advanced project for this week is a web-based version of the improved Mad Lib generator. Look in this week's lecture notes for starter code. It contains the following.

  • madlib.py
  • templates
    • madlib.html - A web form for entering the source of a madlib (like the file in the previous part)
    • words.html - A web form for entering the three missing words
    • result.html - A simple page to display the result

The forms are complete, you must update madlib.py to retrieve data from submitted forms and pass them to the next form. The expected flow for a user is:

  1. The user loads the starting page (url: /)
  2. The madlib form is submitted to the words page (url: /words)
  3. The words form is submitted to the result page (url: /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.

If you do the advanced project submit the following files:

  1. madlib.py
  2. templates
    1. madlib.html
    2. words.html
    3. result.html

Also, submit the answer to the question.

Grading

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