Python by Projects

unit

Story Machine

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.

You will learn

  • 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!")

Output

The Story Machine is awake!

Notice

print() shows whatever sits inside the quotes.

print("First, the dragon yawned.")
print("Then it sneezed glitter.")

Output

First, the dragon yawned.
Then it sneezed glitter.

Notice

Each print() starts its own new line, top to bottom.

print("Chapter One")
print()
print("A sleepy dragon borrowed my backpack.")

Output

Chapter One

A sleepy dragon borrowed my backpack.

Notice

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.")

Output

A nervous llama opened the secret door.

Notice

Everything between the quotes is the text that gets shown.

print('Behind it waited a mountain of jellybeans.')

Output

Behind it waited a mountain of jellybeans.

Notice

You may use 'single' or "double" quotes — just start and end with the same kind.

print("Careful — it's a trap!")

Output

Careful — it's a trap!

Notice

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!")

Output

The Story Machine is warming up!

Notice

Python skips the # line and runs only the print().

print("Loading the story...")  # a friendly reminder for whoever reads the code

Output

Loading the story...

Notice

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.

Read the error

print("The penguin wore a purple wig.)

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.")

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)

Output

Captain Pickle

Notice

The name hero now stands for the saved words, so print(hero) shows them.

hero = "Captain Pickle"
place = "the moon library"
print(hero)
print(place)

Output

Captain Pickle
the moon library

Notice

You can save as many things as you like, each in its own name.

mood = "sleepy"
print(mood)
mood = "thrilled"
print(mood)

Output

sleepy
thrilled

Notice

Saving 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)

Output

Professor Pancake

Notice

hero_name tells you what it holds — far better than a vague name like stuff.

silly_object = "a squeaky crown"
print(silly_object)

Output

a squeaky crown

Notice

Join words in a name with _ — spaces are not allowed inside a name.

favorite_snack = "pickle pizza"
print(favorite_snack)
print(favorite_snack)

Output

pickle pizza
pickle pizza

Notice

Spell 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 yourself

name = input("Your name? ")
print(name)

Notice

input() waits, then saves your reply in the variable.

Try it yourself

color = input("Name a color: ")
print("You chose this color:")
print(color)

Notice

The saved reply is a string like any other — reuse it with print().

Try it yourself

hero = input("Name a hero: ")
sidekick = input("Name a sidekick: ")
print(hero)
print(sidekick)

Notice

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 + "!")

Output

Meet Captain Pickle!

Notice

+ glues the pieces into a single message.

hero = "Captain Pickle"
place = "the moon"
print(hero + " lives on " + place)

Output

Captain Pickle lives on the moon

Notice

You can join several saved strings and literals in a row.

word = "jelly"
other = "bean"
print("stuck: " + word + other)
print("spaced: " + word + " " + other)

Output

stuck: jellybean
spaced: jelly bean

Notice

+ 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 f before the opening quote and each variable name inside curly braces.

hero = "Captain Pickle"
print(f"{hero} saved the day.")

Output

Captain Pickle saved the day.

Notice

The f lets {hero} become the saved words inside the sentence.

hero = "Captain Pickle"
place = "the moon library"
print(f"{hero} raced to {place}.")

Output

Captain Pickle raced to the moon library.

Notice

Drop in as many {names} as you like.

hero = "Captain Pickle"
print(f"{hero} tripped, but {hero} jumped right back up!")

Output

Captain Pickle tripped, but Captain Pickle jumped right back up!

Notice

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}.")

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.

Notice

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 yourself

# 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}.")

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.

Recap

  • 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 SyntaxError for 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.

Go to the exercises (9)

For parents and teachers: report a problem