thecodex.expert · The Codex Family of Knowledge
Tier 0 · Absolute Beginner · Python Project

Mad Libs Generator

Collect funny words from the player, then drop them into a story for a silly result. Your first real work with text.

🧠 Teaches how to think spoonfed, every age Last verified:

1 The Problem

We want the classic word game: ask the player for a noun, a verb, an adjective, and so on, then slot their words into a pre-written story. The result is usually hilarious. It teaches collecting several inputs and building a string from pieces.

Where this shows up: any program that builds text from parts — personalised emails, form letters, templated messages, auto-generated reports, chatbot replies. “Fill the blanks in a template” is a hugely common task.

2 How to Think About It

The plan, before any code:

The plan — in plain English
1. Ask the player for each word the story needs (a name, a place, a verb…). → 2. Store each answer in its own labelled box (a variable). → 3. Slot the words into the story template. → 4. Print the finished, silly story.

Ask for a name

Ask for a place

Ask for a verb

Ask for an adjective

Slot words into the story

Print the finished story

3 The Build — explained part by part

Here is the complete Mad Libs game. Each line is explained below.

Pythonmadlibs.py
# Collect some words from the player.
name = input("Enter a name: ")
place = input("Enter a place: ")
verb = input("Enter a verb (an action): ")
adjective = input("Enter an adjective (a describing word): ")

# Slot the words into a story template.
story = f"""
One day, {name} decided to visit {place}.
On the way, they could not stop {verb}!
Everyone agreed it was the most {adjective} day ever.
"""

print(story)
What each part does — in plain words
name = input("Enter a name: ") — ask the player for a word and store their answer in a labelled box called name. We do this once for each word the story needs.

The triple-quoted f-string (f"""…""") — triple quotes let a piece of text span several lines. The f at the front means “fill in the {boxes} with the variables”. So {name} becomes whatever the player typed.

print(story) — show the finished story with all the words slotted in. Each {word} in the template is replaced by the player’s answer.
Common mistakes — and how to avoid them
✗ Forgetting the f before the quotes — then {name} prints literally instead of the player's word.
✓ Always put f right before the opening quote to enable {box} filling.
✗ Misspelling a variable inside the {}{nmae} causes an error.
✓ Make sure the name inside {} matches the variable exactly.
✗ Using single quotes for a multi-line story — that breaks across lines.
✓ Use triple quotes """ for text that spans several lines.

4 Test & Prove Each Part

The story is just text-filling, so we test that the player’s words actually end up in the finished story.

The chosen name appears in the story
The chosen place appears in the story
All four words make it into the result
Pythontest_madlibs.py
def make_story(name, place, verb, adjective):
    """Build the Mad Libs story from the given words."""
    return (f"One day, {name} decided to visit {place}. "
            f"On the way, they could not stop {verb}! "
            f"Everyone agreed it was the most {adjective} day ever.")


def test_name_in_story():
    """The name appears in the finished story."""
    story = make_story("Sam", "Paris", "laughing", "amazing")
    assert "Sam" in story


def test_place_in_story():
    """The place appears in the finished story."""
    story = make_story("Sam", "Paris", "laughing", "amazing")
    assert "Paris" in story


def test_all_words_present():
    """Every word the player gave appears in the result."""
    story = make_story("Sam", "Paris", "laughing", "amazing")
    for word in ("Sam", "Paris", "laughing", "amazing"):
        assert word in story

Run with pytest -v. We moved the story-building into a make_story function so tests can check the words land in the text — without needing a human to type them.

5 The Interface

INPUTfour wordsname, place, verb, adjective
What it expects
Enter a name: Sam
Enter a place: the moon
Enter a verb: dancing
Enter an adjective: sparkly
OUTPUTthe storywords slotted in
What it returns
One day, Sam decided to visit the moon.
On the way, they could not stop dancing!
Everyone agreed it was the most sparkly day ever.

6 Run It & Automate It

Run it locally
python3 madlibs.py
Answer each prompt and read your silly story.

Jenkins runs the tests automatically — each line explained below.

What you should see when it works
Terminala real run
Enter a name: Sam
Enter a place: the moon
Enter a verb (an action): dancing
Enter an adjective (a describing word): sparkly

One day, Sam decided to visit the moon.
On the way, they could not stop dancing!
Everyone agreed it was the most sparkly day ever.
If it breaks — how to fix it
🚨 NameError: name ... is not defined.
A word inside {} does not match any variable. Check the spelling.
🚨 The braces show literally like {name}.
You forgot the f before the opening quote.
GroovyJenkinsfile
// Jenkinsfile — automated tests on every change.
pipeline {
    agent any
    stages {
        stage('Get the code') { steps { checkout scm } }       // download the code
        stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } }  // test tool
        stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } }   // run every test
    }
    post {
        success { echo 'All tests passed.' }
        failure { echo 'A test failed — look above.' }
    }
}
🎯 Try this next — make it yours
  1. Longer story. Add more blanks and a second paragraph. (Teaches: more variables.)
  2. Pick a random story. Have two templates and choose one at random. (Teaches: random.choice.)
  3. Count the words. Tell the player how many words their story has. (Teaches: len and split.)
What you learned
You learned to collect several inputs, store each in its own variable, and build multi-line text with a triple-quoted f-string — the foundation of templated messages everywhere. Related: f-strings, String Methods.