Python Mad Libs Workshop

Workshop #6 - Creating a Mad Libs Game with Python

Today we’re going to create a fun Mad Libs game using Python! Mad Libs are word games where you fill in blanks with different types of words (like nouns, verbs, or adjectives) to create a silly story. Let’s make our own!

What You’ll Need

  1. A computer with Python installed (or use an online Python editor like Trinket)
  2. Your imagination!

Step 1: Printing in Python (10 minutes)

Let’s start by learning how to print text in Python. This is how we’ll show our story to the user.

# Try these examples:
print("Hello, World!")
print("Welcome to our Mad Libs game!")
print("This is a story about a magical adventure!")

# You can print multiple things on one line using commas
print("Once upon a time,", "there was a magical cat.")

# You can also print numbers
print(5)
print(3 + 2)

Try running these examples and see what happens! The print() function shows text on the screen.

Step 2: Variables and Placeholders (10 minutes)

Now, let’s learn about variables and how to create placeholders for our story. Variables are like containers that can hold different types of information.

# Creating variables
adjective = "silly"
noun = "cat"
verb = "dance"

# Our story template with placeholders
story = """
Once upon a time, there was a {adjective} {noun} who loved to {verb}.
One day, while {verb_ing} in the {place}, they found a {adjective2} {noun2}!
"""

# Filling in the placeholders
story = story.format(
    adjective=adjective,
    noun=noun,
    verb=verb,
    verb_ing="dancing",  # We can put the value directly here
    place="moon",
    adjective2="sparkly",
    noun2="star"
)

# Print the story
print(story)

When you run this code, you’ll see a story with our variables filled in! The {} symbols are placeholders where our variables will go.

Step 3: Getting User Input (10 minutes)

Finally, let’s learn how to get input from the user to make our story interactive!

# Ask the user for words
print("Let's create a silly story!")
print("------------------------")

# Get words from the user
adjective = input("Enter an adjective (like 'silly' or 'sparkly'): ")
noun = input("Enter a noun (like 'cat' or 'robot'): ")
verb = input("Enter a verb (like 'dance' or 'jump'): ")
verb_ing = input("Enter a verb ending in 'ing' (like 'running' or 'singing'): ")
place = input("Enter a place (like 'park' or 'castle'): ")
adjective2 = input("Enter another adjective: ")
noun2 = input("Enter another noun: ")

# Our story template
story = """
Once upon a time, there was a {adjective} {noun} who loved to {verb}.
One day, while {verb_ing} in the {place}, they found a {adjective2} {noun2}!
"""

# Fill in the story with the user's words
story = story.format(
    adjective=adjective,
    noun=noun,
    verb=verb,
    verb_ing=verb_ing,
    place=place,
    adjective2=adjective2,
    noun2=noun2
)

# Print the final story
print("\nHere's your silly story:")
print(story)

The input() function asks the user for information and waits for them to type something. Whatever they type becomes the value of our variable!

How It All Works Together

  1. Printing: We use print() to show text on the screen
  2. Variables: We use variables to store information
  3. Placeholders: We use {} in our story to mark where variables will go
  4. User Input: We use input() to get words from the user
  5. Formatting: We use .format() to put the words into our story

Try It Out!

Run your program and try it with different words. Here’s an example:

Let's create a silly story!
------------------------
Enter an adjective (like 'silly' or 'sparkly'): fluffy
Enter a noun (like 'cat' or 'robot'): penguin
Enter a verb (like 'dance' or 'jump'): hop
Enter a verb ending in 'ing' (like 'running' or 'singing'): dancing
Enter a place (like 'park' or 'castle'): moon
Enter another adjective: sparkly
Enter another noun: star

Here's your silly story:
Once upon a time, there was a fluffy penguin who loved to hop.
One day, while dancing in the moon, they found a sparkly star!

Extra Challenges

Try these fun modifications:

  1. Add more blanks to your story
  2. Create multiple stories and let the user choose which one to fill in
  3. Add colors to your story using the colorama library
  4. Save the stories to a file so you can read them later

What We Learned

Next Steps

Want to learn more? Try these:

Resources