Fill-in-the-blank story games (often called Mad Libs) are the funniest way to learn input() and f-strings. The program asks you for a noun, a verb, an animal and so on, without telling you the story, then drops your words into it. The results are usually ridiculous.
This version picks one of three stories at random and works out which words to ask for by reading the story itself.
Play Silly Stories
Press โถ Play to start the game. When it asks you something, type your answer and press Enter.
View and edit the code (25 lines)
How to play
- Answer each question with one word (or a few).
- Don't think too hard: random words make funnier stories.
- Your story appears at the end. Play again for a different one.
The complete Silly Stories code
Here is the whole program (25 lines). It only uses Python's standard library, so it runs anywhere Python 3 is installed.
import random
import re
STORIES = [
"Today I took my pet {animal} to the {place}. It started to {verb} so "
"{adverb} that everyone gave it a {adjective} {food}.",
"Breaking news: a giant {food} has landed on the {place}! Scientists say "
"it can {verb} faster than a {animal}. People are feeling very {adjective}.",
"My robot learned to {verb} {adverb}. Now it wants to open a {adjective} "
"{place} that only sells {food} to every {animal} in town.",
]
story = random.choice(STORIES)
blanks = re.findall(r"{(\w+)}", story) # the words this story needs
print("SILLY STORIES - give me some words, then read your story!")
words = {}
for blank in blanks:
if blank not in words:
article = "an" if blank[0] in "aeiou" else "a"
answer = input(f"Give me {article} {blank}: ").strip()
words[blank] = answer or "banana"
print("\nHere is your story:\n")
print(story.format(**words))How the Silly Stories game works, step by step
01Write stories with blanks
Each story is a normal string with {names} where the player's words go. Two strings next to each other in brackets are joined automatically, which keeps long lines readable.
story = "Today I took my pet {animal} to the {place}."02Find the blanks automatically
re.findall(r"{(\w+)}", story) finds every word inside curly brackets. Now you can add new stories without changing any other code.
blanks = re.findall(r"{(\w+)}", story)03Ask with good grammar
A tiny check picks an before a vowel and a otherwise, so the program says "an animal" and "a place". Empty answers become "banana".
article = "an" if blank[0] in "aeiou" else "a"04Fill in the story
The answers go in a dictionary, and story.format(**words) puts each one into its blank by name.
print(story.format(**words))What you'll learn
input()and storing answers- f-strings and
.format() - Dictionaries keyed by name
- Conditional expressions (
x if y else z) - A first taste of regular expressions
Python concepts in this game:
Silly Stories coding challenges
Write the key parts of the game yourself. Press Run to try your code and Check to test it. Each challenge you pass earns a star (saved in this browser).
Your first silly story
Ask for a noun and a verb with two input() calls, then print exactly: The noun loves to verb. with the answers in place, e.g. The cat loves to sing.
Hint
Use an f-string: f"The {noun} loves to {verb}."
Show a solution
noun = input("Noun: ")
verb = input("Verb: ")
print(f"The {noun} loves to {verb}.")a or an?
Write article(word) that returns "an" if the word starts with a vowel (a, e, i, o, u, in any case) and "a" otherwise.
Hint
word[0].lower() in "aeiou" checks the first letter.
Show a solution
def article(word):
if word[0].lower() in "aeiou":
return "an"
return "a"
print(article("animal"), article("place"))Fill the blanks
Write fill(story, words) that returns the story with each {name} replaced from the words dictionary. fill("A {x} and a {y}", {"x": "cat", "y": "dog"}) gives "A cat and a dog".
Hint
story.format(**words) does it in one line, or loop over words.items() with .replace().
Show a solution
def fill(story, words):
for name, word in words.items():
story = story.replace("{" + name + "}", word)
return story
print(fill("A {x} and a {y}", {"x": "cat", "y": "dog"}))Make it your own
Once your challenges pass, try one of these upgrades:
- Let players write their own story template.
- Save every finished story to a text file.
- Ask for numbers and plural nouns too.
- Read the story out loud with a text-to-speech library.
Frequently asked questions
How do you make a Mad Libs game in Python?
Write a story with named blanks like {noun}, ask the player for each word with input(), then fill the story with an f-string or str.format() and print it.
What Python concepts does a story game teach?
Variables, input(), string formatting with f-strings and format(), and, in this version, dictionaries and random.choice().
How can I add more stories?
Add another string to the STORIES list. The program finds the blanks in curly brackets by itself, so no other change is needed.