← Back to course
Python 11-12 / Week 01 / Monday
1/6
Week 01 · Back at the Terminal

Monday

The field and the folder
// Review fast, trace before you run, and name the three kinds of error
⏱ about 30 min

Monday: The Field and the Folder

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?"

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

Your fieldsim folder

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.

At your computer
1. Make a new folder called fieldsim, in the place where you keep your schoolwork files.
2. Open IDLE. Choose File, then New File, to open a file window.
3. Choose File, then Save As. Find your fieldsim folder and save the empty file as warmup.py.
Every FieldSim file this course asks for goes in this one folder.
For a grown-up
Python 3, with IDLE, comes from python.org. A grown-up installs it once; Code Crew learners already have it.
Error messages in newer versions of Python can look slightly different from the ones on these pages. No question grades the exact marker lines.
Every program stays in the learner's own fieldsim folder. Nothing in this course needs an account or the internet.

Warm-up: predict, then run

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")
PREDICT WARMUP.PY
  • Read the question.
  • Tap your answer.
What is the first line warmup.py shows?
What is the last line?
How many lines does it show in all?
42 g is light
18 g is light
65 g is heavy
7 g is light
Total: 132 g
At your computer
1. Type warmup.py into your file window. Indent with 4 spaces.
2. Save it, then press F5 to run it. Compare the output with your predictions.
3. Change 50 to 40 in heavy_label. Predict which line changes, then run it.
4. Delete the colon at the end of the for line and run it. Read the message calmly, then put the colon back.

Indentation groups statements

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")
TRACE INDENT_CHECK.PY
  • Read the question.
  • Tap your answer.
How many Total so far lines does it show?
What is the third line?
Total so far: 42 g
Total so far: 60 g
Total so far: 125 g
Total so far: 132 g
StatementTrue 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.?
WHY THIS EXERCISEIDLE comes with Python, F5 restarts the shell and runs the file, and indentation groups statements, with 4 spaces per level.
START A NEW FIELDSIM PROGRAM
  • ?Type the program with 4-space indents
  • ?Open IDLE
  • ?Choose File, then New File
  • ?Save it in your fieldsim folder
  • ?Press F5 to run it
WHY THIS EXERCISERun Module works on a saved file, so the program is typed and saved in the fieldsim folder first.

Your folder is ready and your Python is warm, lead programmer. Tomorrow you will trace programs on paper before you run them.