Introduction: Mad Lib With Python

Making a Mad Libs program in python

What you need:

1. Windows or Mac computer

2. Internet connection

What will you know by the end:

1. Strings

2. Variables

2. Input & print functions

Step 1: Download Python

First you need to download python (obviously). Navigate to, click the download button, and choose the appropriate version for your system.

Step 2: Open IDLE

Once you have downloaded and installed Python, open IDLE. IDLE is the programming environment that we are going to use for this tutorial. There are several other programs we could write python in but this is the basic one packaged with Python itself.

Step 3: Mess Around a Bit

The window that shows up when you first open IDLE can be used as a sort of playground for Python code. When you type a command and hit enter it automatically runs that line and stores whatever values assigned in memory. Go ahead and replicate my code, perhaps with your own name and a couple different ones, to get a basic idea of how everything works. Don't worry if you don't understand it we'll go more in depth in the following steps.

Step 4: Create the Actual Program File

Writing code in the playground is fun, but in order to save a program with the ability to run it on it's own, we need to store the code in a program file. Create a new file to write the program in.

Step 5: Before We Start Writing Code

In order to get input from the user and store it we need to create variables for each of the words we want to store. Think of a variable as you would use one in Algebra. You name the variable on the left side and then assign it to a value using the equal sign. Unlike Algebra, you can store more than just numbers in variables. In the case of this program we will be storing strings. A string is just a word or sentence. Notice that any time text is used it is surrounded by quotes ' '. You can use either single or double quotes as long as the opening one is the same as the closing one. These quotes are not necessary for numbers or variables, only strings.

Step 6: Start Writing Your Program

To start off, let's make a variable for each of the four words we need to get from the user. In order to get input from the user we use input(). By setting the value of each variable to input() we can get input from the user and store them in those variables.

In order to print text to the user we use the command print() and put whatever needs to be printed in the parenthesis. Remember that strings must be surrounded by quotes ' ' but not variable names. Print out the words consecutively by replicating the code in my print function.

Step 7: Run the Program

Now that we have a functioning program go ahead and run it by clicking run then run module. If you haven't saved the file it will ask you to save the file before running it. Do so, then let the program run. You'll notice that nothing prints out, that's because we have only asked the user for input, not actually prompted them with any questions. Go ahead and type 4 words hitting enter in between them to input them, and then make sure the words print out correctly. If they do, go back to the program file and move on to the next step.

Step 8: Adding Prompts to the Input Values

In order to make the input() function have a prompt, we put a string of what we want printed out between the parenthesis. Go ahead and add a prompt to each of the inputs and then run the program to ensure they are working correctly. You'll notice that in mine I put a space after the : before closing with the quote. This is so that when the user types it will not be squished up next to the colon.

Step 9: Create the Output

Since we're adding in the actual print out, go ahead and get rid of the test print function we added earlier. Now in order to output the mad lib correctly there are a couple of things you need to know. First, since we're printing out a poem and want to have it span multiple lines, it's important to note that typing '\n' in a string will skip to the next line. Second, when typing a string you can use the curly braces { } and .format() to insert text into the string. For instance 'I like {0} and {1}'.format('food', 'water') will print out 'I like food and water'. We can use this to our advantage when printing out the mad lib. Replicate the code in the picture in your own program.

Step 10: Run the Program One More Time

Go ahead and run the program one more time to ensure that it is working correctly. Congratulations! You have just written your firs Python program.