Five answers from the user, poured into a story that was written with
holes in it. Nothing here but variables and strings
— and the + operator doing all the heavy lifting.
The program
print("The story machine needs five words. Choose recklessly.")
name = input("A friend's name: ")
animal = input("An animal: ")
adjective = input("A describing word: ")
food = input("A food: ")
number = input("A number: ")
story = name + " woke up to find a " + adjective + " " + animal
story = story + " in the kitchen. It had already eaten " + number
story = story + " plates of " + food + " and showed no sign of stopping."
print()
print("--- YOUR STORY ---")
print(story)
print("The end. Run me again for a different disaster.")Read it before you run it
Predict in writing first — then run the program and grade yourself.
- Count the variables. Which lines create them, and which lines use them?
- The story is built in three steps. Say out loud what
storyholds after the second of the threestory =lines. numberis never converted withint(). Why does this program get away with that, when the guessing game could not?
Make it yours
- One line. Extend the story with one more sentence that reuses a word the user already gave.
- A few lines. Ask for a sixth word — a place, perhaps — and weave it into the story.
- A real change. Write two different endings, then let
random.randint(1, 2)and anifchoose between them. The same words now tell different stories on different runs.
Curriculum connection
C1.3
identify various types of data and explain how they are used within programs
Link to original
C2.1
use variables, constants, expressions, and assignment statements to store and manipulate numbers and text in a program
Link to original
C3.1
analyze existing code to understand the components and outcomes of the code
Link to original