Comet spreads her spiral sketch notebook across the maker space table. On the page are a rectangle of dots, four stars and a few rocks.
"This is the Larkspur test field," she says. "This little box is Pip, our survey rover. I want Pip out on the field today!"
Wren taps his clipboard of graph paper. "What does the evidence say about Pip's routes?" he asks. "We have no evidence yet. Let's test every route in a simulation first."
Nova hovers over the sketch and lights the field up as a grid. "How can I help?" she asks. "A good first step is a folder and a quick warm-up."
Comet turns to you. "You are our lead programmer. Ready to start FieldSim?"
Over twelve weeks the crew will write FieldSim, a text simulator of Pip driving on the Larkspur test field.
The test field, Pip, the samples and every number about them are made up for the story. The Python you write is real.
Each week adds one piece: sample objects, a rover class, a grid map, tests, searching, sorting and more.
IDLE is the editor and shell that comes with Python. In the shell you type one line and see its result straight away.
A file window holds a whole program that you save. Run Module (F5) checks the file, restarts the shell and runs the file.
This program reviews four tools from Code Crew: a function, a list, a loop and an f-string. Read it and predict before you type it.
# warmup.py
# A quick review: a function, a list, a loop and an f-string.
def heavy_label(grams):
if grams >= 50:
return "heavy"
return "light"
grams_list = [42, 18, 65, 7]
total = 0
for g in grams_list:
total += g
print(f"{g} g is {heavy_label(g)}")
print(f"Total: {total} g") 42 g is light 18 g is light 65 g is heavy 7 g is light Total: 132 g
Python uses indentation to group statements. In warmup.py, the two lines indented under for are the loop body.
The style guide says to indent each level with 4 spaces, and not to use tabs.
Moving one line in or out changes what the program does. This version moves the print line inside the loop.
# indent_check.py
grams = [42, 18, 65, 7]
total = 0
for g in grams:
total += g
print(f"Total so far: {total} g") Total so far: 42 g Total so far: 60 g Total so far: 125 g Total so far: 132 g
| Statement | True or false? |
|---|---|
| IDLE is the editor and shell that comes with Python. | ? |
| Run Module (F5) restarts the shell before it runs the file. | ? |
| Indentation is only for looks; Python ignores it. | ? |
| The style guide says to indent with tabs. | ? |
Your folder is ready and your Python is warm, lead programmer. Tomorrow you will trace programs on paper before you run them.