Exit slides Esc

Story Machine

Slide 1 of 43

  1. 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.

  2. 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.

  3. print("The Story Machine is awake!")

    Output

    The Story Machine is awake!
    

    print() shows whatever sits inside the quotes.

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

    Output

    First, the dragon yawned.
    Then it sneezed glitter.
    

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

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

    Output

    Chapter One
    
    A sleepy dragon borrowed my backpack.
    

    An empty print() leaves a blank line — handy for spacing a story out.

  6. The text inside matching quotes is a string literal. The quotes show Python where the text begins and ends.

  7. print("A nervous llama opened the secret door.")

    Output

    A nervous llama opened the secret door.
    

    Everything between the quotes is the text that gets shown.

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

    Output

    Behind it waited a mountain of jellybeans.
    

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

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

    Output

    Careful — it's a trap!
    

    Punctuation like —, !, and ' is part of the text too; Python shows it exactly.

  10. A comment is a note for people who read the program. Python does not print it. A comment begins with #.

  11. # This note explains the next line.
    print("The Story Machine is warming up!")

    Output

    The Story Machine is warming up!
    

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

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

    Output

    Loading the story...
    

    A comment can also sit at the end of a line, after the code.

  13. Programs sometimes break. That is normal. Run this broken line on purpose, then read the error message from the bottom.

  14. Error demo: this code stops with an error
    print("The penguin wore a purple wig.)
  15. 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.

  16. print("The penguin wore a purple wig.")

    Output

    The penguin wore a purple wig.
    
  17. 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.

  18. hero = "Captain Pickle"
    print(hero)

    Output

    Captain Pickle
    

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

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

    Output

    Captain Pickle
    the moon library
    

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

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

    Output

    sleepy
    thrilled
    

    Saving a new value replaces the old one — the most recent value wins.

  21. A clear variable name helps you remember what it saves. Use letters and underscores, no spaces, and the exact same spelling every time.

  22. hero_name = "Professor Pancake"
    print(hero_name)

    Output

    Professor Pancake
    

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

  23. silly_object = "a squeaky crown"
    print(silly_object)

    Output

    a squeaky crown
    

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

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

    Output

    pickle pizza
    pickle pizza
    

    Spell the name the exact same way each time, or Python will not find it.

  25. 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.

  26. Try it
    name = input("Your name? ")
    print(name)

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

  27. Try it
    color = input("Name a color: ")
    print("You chose this color:")
    print(color)

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

  28. Try it
    hero = input("Name a hero: ")
    sidekick = input("Name a sidekick: ")
    print(hero)
    print(sidekick)

    Ask as many questions as your story needs; each reply gets its own variable.

  29. Lesson 3: Combine Words into a Story

    String concatenation joins string literals and saved strings with +, making one longer message.

  30. hero = "Captain Pickle"
    print("Meet " + hero + "!")

    Output

    Meet Captain Pickle!
    

    + glues the pieces into a single message.

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

    Output

    Captain Pickle lives on the moon
    

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

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

    Output

    stuck: jellybean
    spaced: jelly bean
    

    + adds no spaces on its own — put a " " between the pieces where you want a space.

  33. 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.

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

    Output

    Captain Pickle saved the day.
    

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

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

    Output

    Captain Pickle raced to the moon library.
    

    Drop in as many {names} as you like.

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

    Output

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

    The same {name} can appear more than once.

  37. 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.

  38. # 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.
    
  39. 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.

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