Checking your account

You can read the lesson while its saved answers and constructions load. Learning controls will open once that work is ready.

Python Validation and Error Handling

Close the Buffer Even When a Line Is Bad

Coding challenges

BOTIE

Open the buffer and read its lines. Inspect the values or error while closed is still false.
Ready to test
0 passed · 0 failed · 3 not run

An empty floor. Start with the information your machine needs.

Received result

Your answer will arrive here.

Examples, experiments and workspace controls
Live values

Connect a tool's inputs to see what it produces. You can try a branch before connecting it to Result.

Try your own inputs

Use a copy of the example to try your own data. Keep the problem's assumptions in mind, and predict the effect of a change before checking the output. You can also add another branch to compare two ways of doing the calculation.

Start at an output arrow, then click or drag to an input. Drag empty space to pan. Delete removes a selection. Ctrl/Cmd+Z undoes a change.
Context for this challenge

Open the buffer and read its lines. Inspect the values or error while closed is still false.

The text contains 4, a blank line, 7 and 3. Reading its whole-number lines gives a total of 14. The buffer must close afterward, including when a later example contains unreadable text.

Inspect the component calculation
Read the number lines(
  Open buffer: Open the text buffer(
    Lines as text: Buffer contents
  )
)
Examples and results
Buffer contents
4

7
3
Required result
lines
  1. 0"4"
  2. 1""
  3. 2"7"
  4. 3"3"
  5. 4""
values
  1. 04
  2. 17
  3. 23
closed
false
error
null
Buffer contents
10
-2
5
Required result
lines
  1. 0"10"
  2. 1"-2"
  3. 2"5"
  4. 3""
values
  1. 010
  2. 1-2
  3. 25
closed
false
error
null
Buffer contents
""
Required result
lines
  1. 0""
values
[]
closed
false
error
null
Try a fresh construction

This clears this draft, including its undo history. Earlier successful attempts stay recorded. Build again without opening hints to record an independent attempt.

Stuck? Inspect a worked construction

The tools below form one possible solution. You can assemble it, follow its signals, and undo that change to return to your own construction.

Assumptions and test precision

This local buffer model carries an error outcome to the closing piece. Python challenges exercise actual exceptions and with statements. No file is opened by this workshop. Numerical tests allow a difference of 0.001. Lists and structures must also have the right shape.

Your last tested pieces can start the full construction. An existing draft there keeps its own pieces.

The Failure Happens Later

This check uses the pieces from “Read, then Close on Either Result”.

BOTIE

The new buffer contains 4, 7, 9 and unknown on separate lines. The read fails on its fourth line. Apply the same resource-lifetime rule.
Buffer contents
4
7
9
unknown
Should the buffer be closed after the fourth line fails?

Saving your prediction runs your current construction on these inputs and checks the earlier examples too.

Build a construction in the workspace above first. This case will test those pieces.

Why does a correct total on the first example tell us little about closing after failure?

Check the resource state on both paths.

This local buffer model carries an error outcome to the closing piece. Python challenges exercise actual exceptions and with statements. No file is opened by this workshop.

Continue the courseTesting Python Functions →
← Mutation and Copies