Make a Mad Libs Story Generator in Python for Kids
KidsKod original illustration.
Have you ever wanted a story where a purple llama dances on the moon? In this project you will build a Mad Libs story generator in Python! Your program asks a friend for silly words, then mixes them into a funny story. If you are brand new to Python, start with Python for Kids: Your First Program first, then come back here.
What you need
- A computer with Python 3 installed, or a free online editor like Trinket or Replit
- About 30 minutes
- A friend or family member to play with (optional, but much funnier!)
What you will learn
- How to ask the player for words with
input() - How to store words in variables
- How to build sentences with f-strings like
f"Hello {name}" - How to use a list and
random.choice()to pick a surprise story - How to repeat the game with a
whileloop
Step 1: Say hello to the player
- Open your Python editor and make a new file called
mad_libs.py. - Type this code and press Run:
print("Welcome to KidsKod Mad Libs!")
name = input("What is your name? ")
print(f"Hi {name}! Let's make a silly story.")
input() waits for the player to type something. The answer is saved in the variable name.
Step 2: Collect silly words
Mad Libs needs different kinds of words. Ask for each one and save it in its own variable:
animal = input("Type an animal: ")
color = input("Type a color: ")
verb = input("Type an action word (like jump or dance): ")
food = input("Type a food: ")
place = input("Type a place: ")
Tip: Explain to younger players that an "action word" is something you can do, like run, sing or wiggle.
Step 3: Build the story with an f-string
An f-string lets you drop variables right into a sentence. Put an f before the quotes and wrap each variable in curly braces:
story = f"One day, {name} met a {color} {animal} at the {place}. The {animal} loved to {verb} while eating {food}!"
print()
print(story)
Run it and enjoy your first silly story!
Step 4: Add more story templates
One story gets boring fast. Let's make a list of stories and let Python pick one at random. Add this line at the very top of your file:
import random
Then replace your story = ... line with this:
stories = [
f"One day, {name} met a {color} {animal} at the {place}. The {animal} loved to {verb} while eating {food}!",
f"Breaking news! A {color} {animal} was seen trying to {verb} in the middle of the {place}. Witnesses say it smelled like {food}.",
f"{name} opened the fridge and found a tiny {animal}. It was {color}, it could {verb}, and it had eaten ALL the {food}!"
]
story = random.choice(stories)
random.choice() picks one item from the list, so every game can be a surprise.
Step 5: Make the story look fancy
Add a border so the story feels like a real storybook page:
print()
print("*" * 40)
print(story)
print("*" * 40)
"*" * 40 repeats the star 40 times. Python can multiply text!
Step 6: Play again with a loop
Wrap your word questions and story in a while loop so players can keep going. Here is the full program:
import random
print("Welcome to KidsKod Mad Libs!")
name = input("What is your name? ")
playing = "yes"
while playing == "yes":
animal = input("Type an animal: ")
color = input("Type a color: ")
verb = input("Type an action word: ")
food = input("Type a food: ")
place = input("Type a place: ")
stories = [
f"One day, {name} met a {color} {animal} at the {place}. The {animal} loved to {verb} while eating {food}!",
f"Breaking news! A {color} {animal} was seen trying to {verb} in the middle of the {place}. Witnesses say it smelled like {food}.",
f"{name} opened the fridge and found a tiny {animal}. It was {color}, it could {verb}, and it had eaten ALL the {food}!"
]
print()
print("*" * 40)
print(random.choice(stories))
print("*" * 40)
playing = input("Play again? (yes/no) ").lower()
print(f"Thanks for playing, {name}!")
The .lower() part turns "YES" or "Yes" into "yes", so the loop still works however the player types it.
Step 7: Test and fix
- Did you get an IndentationError? Make sure every line inside the loop starts with 4 spaces.
- Seeing
{animal}instead of a word? You forgot thefbefore the quotes. - Story never ends? Check that you typed
playing = input(...)inside the loop.
Wrap-up and challenge
Great job! You used input, variables, f-strings, lists, random and a while loop to build a real word game. Now try a challenge:
- Write two more story templates of your own, like a space adventure or a school day gone wrong.
- Ask for a number and use it in the story ("The llama ate 57 pancakes!").
- Make the story shout by using
story.upper()when the player picks "loud mode".
Want another Python game? Try our Python Number Guessing Game for Kids. Happy coding from KidsKod!
