Imagine a ridiculous Mad-Libs story. A heroic pickle may zoom through a library on a singing skateboard. Now you will make the story yours.
You will meet each idea more than once: first the simplest version, then a small twist, then how it looks in a real story. Watch the Notice lines — they point at the one new thing each time.
-
Run a program and show text with
print(), string literals and comments. -
Read an error message from its last line and use its clue to fix the program.
-
Save words in clearly named variables and collect answers with
input(). -
Join words into a story with
+and with f-strings.
-
Lesson 1: Wake up the Story Machine
A program is a set of instructions for the computer. Running a cell tells the computer to follow them.
print()puts a message where you can see it. Here it is three ways, each a small step up.print("The Story Machine is awake!")Scroll the code for more ↓
Output
The Story Machine is awake!print()shows whatever sits inside the quotes.print("First, the dragon yawned.") print("Then it sneezed glitter.")Scroll the code for more ↓
Output
First, the dragon yawned. Then it sneezed glitter.Each
print()starts its own new line, top to bottom.print("Chapter One") print() print("A sleepy dragon borrowed my backpack.")Scroll the code for more ↓
Output
Chapter One A sleepy dragon borrowed my backpack.An empty
print()leaves a blank line — handy for spacing a story out.The text inside matching quotes is a string literal. The quotes show Python where the text begins and ends.
print("A nervous llama opened the secret door.")Scroll the code for more ↓
Output
A nervous llama opened the secret door.Everything between the quotes is the text that gets shown.
print('Behind it waited a mountain of jellybeans.')Scroll the code for more ↓
Output
Behind it waited a mountain of jellybeans.You may use
'single'or"double"quotes — just start and end with the same kind.print("Careful — it's a trap!")Scroll the code for more ↓
Output
Careful — it's a trap!Punctuation like
—,!, and'is part of the text too; Python shows it exactly.A comment is a note for people who read the program. Python does not print it. A comment begins with
#.# This note explains the next line. print("The Story Machine is warming up!")Scroll the code for more ↓
Output
The Story Machine is warming up!Python skips the
#line and runs only theprint().print("Loading the story...") # a friendly reminder for whoever reads the codeScroll the code for more ↓
Output
Loading the story...A comment can also sit at the end of a line, after the code.
Programs sometimes break. That is normal. Run this broken line on purpose, then read the error message from the bottom.
Error demo: this code stops with an error print("The penguin wore a purple wig.)Scroll the code for more ↓
The last line of the traceback names a
SyntaxError. It says the string literal never ended: Python found the opening quote but not its matching closing quote. Add the missing quote and try again.print("The penguin wore a purple wig.")Scroll the code for more ↓
Output
The penguin wore a purple wig.Lesson 2: Save Words to Reuse
The Story Machine needs words it can save and reuse. A variable is a name that saves something for later.
hero = "Captain Pickle" print(hero)Scroll the code for more ↓
Output
Captain PickleThe name
heronow stands for the saved words, soprint(hero)shows them.hero = "Captain Pickle" place = "the moon library" print(hero) print(place)Scroll the code for more ↓
Output
Captain Pickle the moon libraryYou can save as many things as you like, each in its own name.
mood = "sleepy" print(mood) mood = "thrilled" print(mood)Scroll the code for more ↓
Output
sleepy thrilledSaving a new value replaces the old one — the most recent value wins.
A clear variable name helps you remember what it saves. Use letters and underscores, no spaces, and the exact same spelling every time.
hero_name = "Professor Pancake" print(hero_name)Scroll the code for more ↓
Output
Professor Pancakehero_nametells you what it holds — far better than a vague name likestuff.silly_object = "a squeaky crown" print(silly_object)Scroll the code for more ↓
Output
a squeaky crownJoin words in a name with
_— spaces are not allowed inside a name.favorite_snack = "pickle pizza" print(favorite_snack) print(favorite_snack)Scroll the code for more ↓
Output
pickle pizza pickle pizzaSpell the name the exact same way each time, or Python will not find it.
input()pauses the program, shows a prompt, and saves what you type into a variable. These cells wait for you to type, so run them yourself and type an answer.Try it name = input("Your name? ") print(name)Scroll the code for more ↓
input()waits, then saves your reply in the variable.Try it color = input("Name a color: ") print("You chose this color:") print(color)Scroll the code for more ↓
The saved reply is a string like any other — reuse it with
print().Try it hero = input("Name a hero: ") sidekick = input("Name a sidekick: ") print(hero) print(sidekick)Scroll the code for more ↓
Ask as many questions as your story needs; each reply gets its own variable.
Lesson 3: Combine Words into a Story
String concatenation joins string literals and saved strings with
+, making one longer message.hero = "Captain Pickle" print("Meet " + hero + "!")Scroll the code for more ↓
Output
Meet Captain Pickle!+glues the pieces into a single message.hero = "Captain Pickle" place = "the moon" print(hero + " lives on " + place)Scroll the code for more ↓
Output
Captain Pickle lives on the moonYou can join several saved strings and literals in a row.
word = "jelly" other = "bean" print("stuck: " + word + other) print("spaced: " + word + " " + other)Scroll the code for more ↓
Output
stuck: jellybean spaced: jelly bean+adds no spaces on its own — put a" "between the pieces where you want a space.An f-string is another way to mix saved strings into story text. Put
fbefore the opening quote and each variable name inside curly braces.hero = "Captain Pickle" print(f"{hero} saved the day.")Scroll the code for more ↓
Output
Captain Pickle saved the day.The
flets{hero}become the saved words inside the sentence.hero = "Captain Pickle" place = "the moon library" print(f"{hero} raced to {place}.")Scroll the code for more ↓
Output
Captain Pickle raced to the moon library.Drop in as many
{names}as you like.hero = "Captain Pickle" print(f"{hero} tripped, but {hero} jumped right back up!")Scroll the code for more ↓
Output
Captain Pickle tripped, but Captain Pickle jumped right back up!The same
{name}can appear more than once.Now build the full Mad-Libs Story Machine. Run it first with fixed words, then the asking version. Both save ideas in clearly named variables and reuse them in the story.
# Use fixed story ingredients so this version runs without stopping. hero = "Captain Pickle" place = "the moon library" object_name = "a squeaky crown" action = "yodel" # Print the title and the story. print("The Amazing Adventure of " + hero) print(f"One day, {hero} arrived at {place}.") print(f"A mysterious voice offered {hero} {object_name}.") print(f"Without warning, {hero} began to {action}!") print(f"Everyone at {place} cheered for {hero}.")Scroll the code for more ↓
Output
The Amazing Adventure of Captain Pickle One day, Captain Pickle arrived at the moon library. A mysterious voice offered Captain Pickle a squeaky crown. Without warning, Captain Pickle began to yodel! Everyone at the moon library cheered for Captain Pickle.The fixed words let this full story run here. The asking version below uses the same five print lines after
input()collects the four story ingredients.Try it # Collect the story ingredients. hero = input("Name a hero: ") place = input("Name a strange place: ") object_name = input("Name a silly object: ") action = input("Name an action: ") # Print the title and the story. print("The Amazing Adventure of " + hero) print(f"One day, {hero} arrived at {place}.") print(f"A mysterious voice offered {hero} {object_name}.") print(f"Without warning, {hero} began to {action}!") print(f"Everyone at {place} cheered for {hero}.")Scroll the code for more ↓
Remix the string literals, prompts, and variable names. Keep matching quotes and exact spellings. If the program stops, read the final traceback line and use its clue. Your Story Machine is now yours.
-
print()shows the text inside matching quotes; a#comment is a note Python skips. -
A traceback's last line names the error, such as a
SyntaxErrorfor a missing quote. -
A variable saves a value under a name, and saving a new value replaces the old one.
-
input()waits for a reply and saves it as a string. -
+joins strings without adding spaces; an f-string puts{name}values inside text.
You can now build and remix your own Mad-Libs Story Machine.
-