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. In this browser, the page cannot pause to ask you for each word: type your five answers, one per line, into the "Input for this program" box before you press Run.
make_story(noun, verb, adjective, place, animal) that returns it, and the input() calls and the final print sit in the if __name__ == "__main__": block below it.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. In this browser it returns the next line from the "Input for this program" box instead.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)input() calls. In the project below you will pull the story into make_story(...), which only returns a string, so it can be called with any five words and checked on its own, while the input() calls stay in the __main__ block.#Now build it yourself
Mad Libs: a three-sentence story
Write make_story(noun, verb, adjective, place, animal). It returns a three-sentence story that uses every word at least once. Each sentence must be funny on its own. Under it, ask the user for the five words and print the story. Type the five words, one per line, in the Input box above Run.
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.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.
#Now make it your own
Try one of these variations:
- Two story templates. Write two different story templates and print both with the same five words. Same inputs, two different stories.
- 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.