Learning Goals
3 minBy the end of this lesson you can:
- Turn a short story into a Python template with swappable variables.
- Build multi-sentence output by joining strings with
+. - Run your story over and over with different silly inputs.
Warm-Up
5 minLast lesson we joined, repeated, and measured strings. Today we use those same tools to write a tiny game.
You may know Mad Libs already. One person hides a story, then asks the room: "name a noun!", "give me an animal!", "shout a colour!". They drop those words into hidden blanks. The result is usually nonsense — and very funny.
Try this on paper first. Fill in the two blanks any way you like:
Last Friday, ______ went on a trip and met a ______. They both laughed for hours.
Now we will write that template in Python and run it as many times as we want.
New Concept · Story Templates in Code
12 minPart 1 · The template idea
A template is a story with blanks. The blanks are the bits we want to swap.
- Pick a short story.
- Find the spots where a name, noun, or number could change.
- Replace each spot with a variable at the top of your file.
- When you change a variable, the whole story changes.
Part 2 · From English to Python
Take this hidden-blank sentence:
One day, NAME went to PLACE and ate a ADJECTIVE FOOD.
In Python it becomes:
name = "Aisyah" place = "Penang" adjective = "spicy" food = "laksa" sentence = ("One day, " + name + " went to " + place + " and ate a " + adjective + " " + food + ".") print(sentence)
The brackets around the right-hand side let one statement spread across two lines for readability — handy when concatenations get long.
Part 3 · One sentence at a time
For longer stories, build one sentence per variable. It's neater, and it's easy to add or remove a line later:
line1 = "Once upon a time, " + name + " went exploring." line2 = "They found a glowing " + food + " behind a tree." line3 = "It was the best day ever." print(line1) print(line2) print(line3)
Part 4 · Two Mad-Libs gotchas
- Articles don't auto-fix. Python may print "a elephant" — it doesn't know to switch to "an". Plan around it, or accept the silly grammar as part of the fun.
- Numbers still need
str(). If a story includesageorhours, wrap it:"for " + str(hours) + " hours".
Worked Example · The Great School Trip Disaster
12 minWe will build a three-paragraph silly story. Save this as mad_libs_school.py.
Code
# mad_libs_school.py — a silly school-trip story # 1. Words to swap (try changing these!) hero = "Aiman" place = "Cameron Highlands" adjective = "grumpy" food = "rendang" animal = "elephant" verb = "danced" minutes = 7 silly_word = "blooop" # 2. Build the story, sentence by sentence title = "=" * 40 opener = "Last Friday, " + hero + " went on a school trip to " + place + "." meeting = "On the way, they met a " + adjective + " " + animal + " who was eating " + food + "." shout = "'" + silly_word + "!' shouted " + hero + " in surprise." action = "The " + animal + " " + verb + " for " + str(minutes) + " whole minutes," ending = "then ran off into the jungle, never to be seen again." # 3. Print the story print(title) print(" THE GREAT SCHOOL TRIP DISASTER") print(title) print() print(opener) print() print(meeting) print(shout) print() print(action) print(ending) print() print("THE END") print(title)
Run it. You should see something like:
Output
========================================
THE GREAT SCHOOL TRIP DISASTER
========================================
Last Friday, Aiman went on a school trip to Cameron Highlands.
On the way, they met a grumpy elephant who was eating rendang.
'blooop!' shouted Aiman in surprise.
The elephant danced for 7 whole minutes,
then ran off into the jungle, never to be seen again.
THE END
========================================Now change hero = "Aiman" to hero = "Mei Ling", animal = "elephant" to "orang utan", and food to anything you like. Re-run. A brand-new silly story — and you only edited the variables at the top.
Each variable shows up once at the top, but can be reused many times in the story (notice hero appears twice, animal appears twice). That's the whole point of a template.
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Make three variables: name, colour, and animal. Build a single line and print it:
"Once upon a time, " + name + " met a " + colour + " " + animal + "."
Hint
Three variables, one big + chain, one print().
Take the worked example. Change at least three variables to new silly values — for example, swap the hero, the place, and the food. Run it. Then swap them again to a different set, and run it once more. Read both stories aloud to a friend.
Hint
Stay near the top of the file. The story below should not need any edits.
At the end of the worked example, build a single variable that joins the whole story together using +. Then print how many characters it has using len().
Hint
full_story = opener + meeting + shout + action + ending, then print("Story length:", len(full_story)).
Mini-Challenge · Your Own 4-Sentence Mad Libs
8 minDesign and build your own Mad Libs game from scratch. Pick any theme — a birthday party, a futuristic city, a kampung adventure, or something silly only you can imagine.
Your file must include:
- At least six variables — at least one must be an
int. - A title row using
"*" * something(your choice of length). - A story made of exactly four sentences, each built with
+. - At least one sentence uses
str()to drop the int into the story. - One variable must appear in two different sentences (a recurring character or place).
- A closing row using
"*" * somethingagain.
Stretch goal. After printing the story, count and print how many characters it has using len() on a joined version of the four sentences.
Show one possible solution
# mad_libs_kampung.py — a sleepy-village adventure # Story words hero = "Wei Jie" place = "Kampung Damai" food = "ondeh-ondeh" creature = "kucing" sound = "meow-meow-mwah" hours = 4 # Title print("*" * 36) print(" " + hero + " in " + place) print("*" * 36) # Four sentences line1 = hero + " walked slowly into " + place + " at sunrise." line2 = "A " + creature + " followed " + hero + " all the way to the surau." line3 = "It made a strange " + sound + " sound for " + str(hours) + " whole hours." line4 = "By dinner, the entire kampung had stopped to listen — and to share " + food + "." print(line1) print(line2) print(line3) print(line4) print("*" * 36) # Stretch — total length full_story = line1 + " " + line2 + " " + line3 + " " + line4 print("Story length:", len(full_story), "characters")
Recap
3 minYou built your first text game! Mad Libs is just a story with swappable variables — every tool we used today came from the last five lessons.
Vocabulary Card
- template
- A story with blanks that get filled in with variables.
- Mad Libs
- A word game where words are swapped to make a silly story.
- swappable variable
- A variable at the top of a file whose value changes the output everywhere.
- str()
- Converts a number into a string so it can be joined with
+.
Homework
4 minCreate a new file called adventure.py — a five-sentence Mad Libs adventure.
Rules:
- At least seven variables, including one integer (e.g.,
hoursordistance_km). - A printed title using
"*" * something. - Exactly five sentences, each built with
+. - At least one sentence uses
str()for the integer. - The hero's name must appear in at least three of the five sentences.
- A final line: "This story has X characters." using
len()on a joined version of all five sentences.
Bring it to the next lesson — we'll read out the silliest stories!
Sample · adventure.py
# adventure.py — a tiny jungle adventure # Story words hero = "Aisyah" weapon = "big stick" creature = "monkey" sound = "screech" treasure = "golden roti canai" hideout = "Batu Caves" hours = 3 # Title print("*" * 30) print(" " + hero + "'s adventure") print("*" * 30) # Five sentences line1 = hero + " woke up holding a " + weapon + "." line2 = hero + " stepped into the jungle near " + hideout + "." line3 = "A " + creature + " jumped out with a loud " + sound + "!" line4 = hero + " followed it for " + str(hours) + " hours and found a " + treasure + "." line5 = "She came home with " + treasure + " — and a story to tell." print(line1) print(line2) print(line3) print(line4) print(line5) print("*" * 30) # Story length full_story = line1 + " " + line2 + " " + line3 + " " + line4 + " " + line5 print("This story has", len(full_story), "characters.")
Yours can use any hero, any creature, any kampung, any treasure. As long as the hero appears in three sentences and one sentence uses str(), you've nailed it.