Mini-Project: Mad Libs Generator
Before You Start
#What you'll build
A Mad Libs game that asks the user for five words — a noun, a verb, an adjective, a place, and an animal — and then weaves them into a short, ridiculous story. The user types the words one at a time, presses Enter, and gets back a custom paragraph they can show their friends.
Sample interaction
Welcome to Mad Libs!
Give me a noun: pancake
Give me a verb: dancing
Give me an adjective: sparkly
Give me a place: library
Give me an animal: hamster
--- Your story ---
On a sparkly Tuesday, a hamster went dancing through the library,
clutching a pancake like its life depended on it.
#What you'll learn
After this lesson, you will be able to:
- Read text typed by a person with input() and store it in a named variable
- Hold five separate answers in five variables without losing track of which is which
- Build a multi-line sentence with an f-string, dropping the variables into place
#Step 1: Ask for one word
input() function pauses your program, waits for the user to type and press Enter, then returns whatever they typed as a string.This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
noun = input("Give me a noun: ")
print(f"You said: {noun}")variable = input("prompt: ").What does this print when the user types 42? age = input("Age: ") print(age + 1)
What type does `input()` always return?
#Step 2: Collect all five words
Now repeat the pattern five times, once per word. Give each variable a clear, descriptive name so you can't mix them up in Step 3.
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
print("Welcome to Mad Libs!")
noun = input("Give me a noun: ")
verb = input("Give me a verb: ")
adjective = input("Give me an adjective: ")
place = input("Give me a place: ")
animal = input("Give me an animal: ")That's it. Five variables, five strings, ready to be glued together.
#Step 3: Build the story with an f-string
f. Anything inside curly braces is replaced with the value of that variable. You can even put f-strings on multiple lines using triple quotes.story = f"""
--- Your story ---
On a {adjective} Tuesday, a {animal} went {verb} through the {place},
clutching a {noun} like its life depended on it.
"""
print(story)+, no awkward .format() calls.What gets printed? name = "Alice" print("Hello, {name}!")
f prefix, Python treats the string as plain text. The curly-brace substitution magic ONLY happens in f-strings. Forgetting the f is one of the most common silent bugs — your code runs without error but produces the wrong output. Fix: print(f"Hello, {name}!").What does `f{adjective}` substitute into the string?
#Step 4: Put it all together
Combine all three steps into one script and run it end to end.
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
print("Welcome to Mad Libs!")
noun = input("Give me a noun: ")
verb = input("Give me a verb: ")
adjective = input("Give me an adjective: ")
place = input("Give me a place: ")
animal = input("Give me an animal: ")
story = f"""
--- Your story ---
On a {adjective} Tuesday, a {animal} went {verb} through the {place},
clutching a {noun} like its life depended on it.
"""
print(story)That's a full working program. Try running it with silly inputs — the funnier, the better.
#Now build it yourself
Mad Libs: a three-sentence story
Ask the user for five words (noun, verb, adjective, place, animal), then print a three-sentence story that uses every word at least once. Each sentence must be funny on its own.
Welcome to Mad Libs!
Give me a noun: pancake
Give me a verb: dancing
Give me an adjective: sparkly
Give me a place: library
Give me an animal: hamster
--- Your story ---
On a sparkly Tuesday, a hamster went dancing through the library.
It clutched a pancake like its life depended on it.
By sunset, every sparkly hamster in town was dancing too.# Mad Libs -- three-sentence story
print("Welcome to Mad Libs!")
# TODO: ask for noun, verb, adjective, place, animal
# TODO: build a three-sentence story with f-string
# TODO: print the story
#Now make it your own
Try one of these variations:
- Random story template. Import
randomand pick one of three story templates withrandom.choice(). Same inputs, different stories every run. - Pirate Mad Libs. Write a pirate-themed story with words like "ship", "treasure", and "kraken". Change the prompts to match the theme.
- Two-player Mad Libs. One player provides the words without seeing the template, then the second player reads the final story aloud. Print "Player 1, look away!" before showing the story.