Project 4: Madlibs

In this project you'll take user input to create a mad-lib generator. Your program will use a try/except block to properly handle errors.


You will turn in exercises 11 through 14 with this project.

A Madlib Generator

Like last week, your program will use the input() function to take user input. This week you'll use command line inputs and runtime input:

  1. A madlib with embedded formatting markers {} will be passed on the command line
  2. A noun will be read using input()
  3. A verb will be read using input()
  4. An adjective will be read using input()

You will the produce the completed madlib. Here's what your program should look like when executed:

python3 ./ 'I like to {verb} {adjective} {noun}.'
Enter a noun: turtles
Enter a verb: program
Enter adjective: purple
I like to program purple turtles.

Handling Errors

Your program should handle errors gracefully by surrounding the print() function with a try/except block. You can see how to do that in the class notes. Here's an example of an error and how it should be handled:

$ python3.6 ./ 'I like to {verb} {adverb} {noun}.'
Enter a noun: turtles
Enter a verb: program
Enter adjective: purple
Sorry, there was an error.

Test your program to be sure that I don't see an exception!

Web-Based Madlib (Advanced)

The advanced project for this week is the same Madlib generator, but on the web. The requirements are:

  1. The web-based madlib gets the madlib input from an environment variable named MADLIB
  2. Use a web form to input the verb, adverb and noun
  3. Display the completed madib upon submission of the form

Your web-madlib generator will be executed like this:

$ export MADLIB='I like to {verb} {adjective} {noun}.' 
$ python3.6 


  • All files must have header comments.
  • Submit your program in a file called
  • The project has the following requirements:
    • You must take three inputs (noun, verb, adjective)
    • You must print the completed madlib on a line by itelf.
    • If there's an error you must output an error message that contains the word “sorry”
  • If you do the web-based version submit the following:
    • A directory called project4
      • Your program in project4/
      • Your templates in project4/templates
      • Your templates can have any names you choose
    • Your program must use the 'MADLIB' environment variable (not the command line)


  • 5 points for the exercises
  • 5 points for a correct submission (filenames, comments, etc)
  • 10 points for the correctness of your program.