Build a Mad Libs Game in Python
Code a funny Mad Libs word game in Python. Collect nouns, verbs and adjectives with input, slot them into a story with f-strings, and print a silly tale. Full runnable code, a sample game and a quiz.
Key takeaways
- input() collects words from the player and stores them in variables
- f-strings slot variables straight into a story template
- A list of prompts plus a loop keeps the code short and easy to extend
- The same template gives a brand-new story for every set of words
- Mad Libs is a fun way to practise variables, input and string formatting
Build a silly story machine
Mad Libs is a word game: you supply words — a noun, a verb, an adjective — without seeing the story, then they get slotted in to make something hilarious. It's a perfect Python project because the whole game is really just collecting words and putting them into a template. You'll practise variables, input() and string formatting, and end up with something genuinely fun to share.
If you've done python input and output, you're ready. Let's build it.
Step 1: collect a word
Each word the player gives is saved in a variable with input():
adjective = input("Give me an adjective: ")
animal = input("Give me an animal: ")
Whatever the player types is stored, ready to drop into the story later.
Step 2: slot words into a story with f-strings
An f-string is a string with the letter f before the opening quote. Anything inside { } is replaced by a variable's value:
story = f"The {adjective} {animal} danced all night long!"
print(story)
If adjective is "sneaky" and animal is "penguin", this prints The sneaky penguin danced all night long! (More on f-strings in working with text and strings.)
Step 3: collect many words without repeating yourself
Asking for ten words with ten input() lines gets repetitive. Instead, keep the prompts in a list and loop:
prompts = ["a name", "an adjective", "a verb", "a place", "a number"]
answers = []
for prompt in prompts:
word = input(f"Give me {prompt}: ")
answers.append(word)
Each answer lands in the answers list in order, so answers[0] is the name, answers[1] the adjective, and so on. (See python for loops for how looping works.)
The complete Mad Libs game
Here's the full program. It collects words into named variables, then prints a complete silly story:
print("=== MAD LIBS: A Day at School ===")
print("Answer without reading the story. Trust me — it's funnier!\n")
name = input("Enter a name: ")
adjective1 = input("Enter an adjective: ")
animal = input("Enter an animal: ")
verb = input("Enter a verb (past tense, e.g. 'jumped'): ")
food = input("Enter a food: ")
adjective2 = input("Enter another adjective: ")
number = input("Enter a number: ")
story = f"""
Today {name} brought a {adjective1} {animal} to school.
During lunch it {verb} onto the table and ate {number} plates of {food}.
The teacher said it was the most {adjective2} thing she had ever seen!
"""
print("\n=== YOUR STORY ===")
print(story)
again = input("Play again? (yes/no): ").lower()
if again == "yes":
print("Run the program again for a brand new tale!")
How it works:
- Each
input()collects one word into a clearly named variable, so it's easy to place correctly. - The triple-quoted
f"""..."""lets the story span several lines while still slotting in every{variable}. - Because the player types fresh words each time, the same template produces a different story every game.
A sample game:
=== MAD LIBS: A Day at School ===
Enter a name: Maya
Enter an adjective: grumpy
Enter an animal: octopus
Enter a verb (past tense, e.g. 'jumped'): sneezed
Enter a food: pancakes
Enter another adjective: sparkly
Enter a number: 99
=== YOUR STORY ===
Today Maya brought a grumpy octopus to school.
During lunch it sneezed onto the table and ate 99 plates of pancakes.
The teacher said it was the most sparkly thing she had ever seen!
Try it yourself
Make your Mad Libs even better:
- Write your own story. Replace the template with a tale of your choice. Add as many
{blanks}as you like — just collect a word for each. - Many stories. Keep two or three templates in a list and use
random.choiceto pick one, so the structure is a surprise too. See random numbers and games in Python. - Use the list version. Switch to the
promptslist andanswerslist approach so adding a new blank is just adding to a list. - Save the funniest. Append finished stories to a file so you can read your best ones later with reading and writing files in Python.
Quick quiz
Test yourself and earn XP
Which function asks the player to type a word?
input() shows a prompt and returns whatever the player types.
What is an f-string?
f"Hello {name}" inserts the value of name straight into the text.
In f"The {animal} jumped", what fills {animal}?
Inside an f-string, {animal} is replaced by the current value of the variable.
Why store each answer in its own variable?
Each word is saved so the story template can use it where it belongs.
How can we avoid repeating input() many times?
A list of prompts plus a for loop collects all the words with far less code.
FAQ
The story template stays the same, but because the player types new words every game, the result changes completely. You can also keep several templates in a list and use random.choice to pick one, so the structure of the joke is a surprise too.
Yes — input() captures everything typed until Enter, so 'big purple dragon' works fine wherever an adjective or noun is expected. That often makes the story even funnier. If you want to be stricter, you could check the answer with .split() to count the words, but for Mad Libs, anything goes.
Keep exploring
More in Coding