Project 1

Mad Libs

The Project

Welcome to your first project in CS 3-4!

In this project, you’ll be using IDLE to write a program that lets the user fill in a mad lib. When you’re done, your program will print a story using their inputs, something like this:

   Welcome to my mad lib program!                                                            
   Please give me an adjective: friendly                                                     
   Please give me an animal: dog                                                             
   Please give me an emotion: happy                                                          
   Please give me a color: blue                                                              
   Please give me a food: pizza     
                                                         
   The friendly dog feels happy when it eats blue pizza.                                                                                                                                                           

This is just a simple example of a mad lib script - you should write a better one!


Starter Code

This project comes with some starter code to get you started. Download this file and open it in IDLE.

The starter code comes with a mad_lib function - that’s where your code should go.

The starter code also has some comments with the word TODO in them, which are just reminders about what you’re supposed to do. Delete those TODO comments once you’ve done the things that they tell you to do.


Requirements

You will build a mad_lib() function that, when called, will prompt the user for input of at least 5 words and then plug them into a mad-lib script you’ve written. If you’d like to write a longer mad-lib, go ahead and prompt the user for lots more words!

Remember: here’s how to use the input function to ask the user for a string of text and then save that string to a variable:

   word = input("I would like one word, please: ")

You will then combine this user input with text to make your story inside the given mad_lib function.


credit your source

If you write your own original story, credit YOURSELF in a comment (# story by Tamara O'Malley)

If you got a story from somewhere, be sure to credit your source in a comment (# nursery rhyme, # lyrics to The Boxer by Paul Simon, ...). Include a link to the website if you found it online.


A couple Suggestions

build lines of story

IDLE doesn't pay attention to the ends of words, so it might be nice to set up your story with line breaks. One way to do this is to use variables to save each line of the story:

   line1 = "The " + adjective1 + " dog + " feels \n" 
   line2 = emotion + " when it eats " + color + "\n"

Recall that \n can be used inside a string to create a line return.

Then when you build and return the entire story, you can do it like this:

   return line1 + line2


use a get_word function

Also, you’re probably going to end up writing a lot of code like this:

   adjective = input("Please give me an adjective: ")
   noun = input("Please give me a noun: ")
   animal = input("Please give me an animal: ")

That can get pretty repetitive. If you want to make this a little better, you could write a function that takes an argument of the kind of word you want:

   get_word("an adjective")

Remember, when writing the function, the parameter will be a variable name that represents the argument:

   def get_word(kind_of_word):

Inside the function you should RETURN a user input statement (weird, right?):

   return input("Please give me " + kind_of_word + ":")

Then you can use that function to collect user input:

   adjective = get_word("an adjective")
   noun1 = get_word("a noun")
   animal = get_word("an animal")
   noun2 = get_word("another noun")

I like this better because it means that I don’t have to type out "Please give me a <something>:" every time.

You don’t have to do this get_word thing, but you can if you want to!


Submitting your project

On Google Classroom, submit a file called mad_lib_YOUR_NAME.py.

For example, I’d submit a file called mad_lib_TAMARA_OMALLEY.py.

On the first line of that file, double check that you wrote a comment with your name on it, like this:

 # Tamara O'Malley


The projects in this class were created with a LOT of help from JR Heard, a TEALS volunteer at Madison, 2017-2019. His version of this project lives at https://blog.jrheard.com/python/mad-libs.