Skip to content
Courses

Python Behavior Lab

Programming FoundationsLab 1 of 3 · Guided75 minutesLabBuilds on: Functions, Errors, and Files

Finish the three preceding Python lessons before you start this one, and work inside the Ubuntu x86-64 course VM with Python 3, tar, and sha256sum all available. Everything here is local, and the only thing that needs internet access is the download itself. Keep that verified archive completely untouched, because it’s your reset point and you’ll probably want it.

Download python-behavior-v1.0.0.tar into ~/underdunn-course/downloads, where its expected SHA-256 is:

531448744b567edabd6acd8a7a6fca9f5fd7d8f7d185295228574ec0fa49fd61
Terminal window
cd ~/underdunn-course/downloads
sha256sum python-behavior-v1.0.0.tar
mkdir -p ~/underdunn-course/labs/python-behavior
tar -xf python-behavior-v1.0.0.tar -C ~/underdunn-course/labs/python-behavior
cd ~/underdunn-course/labs/python-behavior/python-behavior-v1.0.0
find . -maxdepth 2 -type f -printf '%P\n' | sort

You should see behavior.py and values.txt come back from that. And stop right there if the digest differs, or if either of those files is missing.

Begin with where the input path enters the program and where the final result is printed. Then work inward through the operations connecting them. The earlier lessons gave you the pieces: an argument supplies text, opening a file supplies lines, conversion supplies numbers, and a condition determines which numbers affect state. Your map should show the order this particular program uses, rather than assuming it matches an earlier example.

Read behavior.py and values.txt, and work out which lines reach the count update. A quick scratch trace may help, but no completed table is required. Then run:

Terminal window
python3 behavior.py values.txt

Compare the selected count with the comparison and update in the source.

Milestone 2: check the path through the loop

Section titled “Milestone 2: check the path through the loop”

Pick a selected value and a line that leaves the count unchanged. Follow each through stripping, conversion and the condition. If your predicted total differs from the output, use nl -ba values.txt to find the first line where your count diverges.

A line skipped before conversion never becomes a number. A valid number rejected by the comparison reaches that test but leaves the count alone. Keep those paths distinct.

Add an invalid line to a copy of the input, and then explain the traceback that comes back. Don’t modify the original file, since it’s the thing everything else is measured against.

Terminal window
cp values.txt values-invalid.txt
printf 'not-a-number\n' >> values-invalid.txt
python3 behavior.py values-invalid.txt
printf 'status=%s\n' "$?"

Check the final exception, the first frame inside behavior.py itself, the input line responsible for it, and the exit status. Then remove values-invalid.txt, and nothing else in that directory.

Keep the two line numbers distinct. The traceback locates an operation in the Python source; your numbered input identifies the data that reached that operation. Use the two line numbers to find the failing operation and its input. A conversion failure establishes that this text could not be interpreted as the requested number. It does not establish that the later comparison ran or that the program completed its final summary.

Enter the comparison threshold from the source and the selected count you checked with the original input.

Check your answers

Enter the values you found. Your answers are checked in this browser without being sent anywhere.

Hint 1: strategyFind the comparison constant first, then count only the values whose branch actually reaches the state update.
Hint 2: evidenceTrace the loop once per input line, and check the conversion before you check the comparison.
Hint 3: explicit actionOpen values.txt and behavior.py side by side in Text Editor, complete one trace row per input line, and then confirm the whole thing with python3 behavior.py values.txt.
  • The checksum doesn’t match: delete only that download, fetch the pinned link again, and recheck it. Don’t extract a mismatched archive.
  • FileNotFoundError: run pwd and ls -la, and then check the supplied relative path against them.
  • Your count differs from the output: find the first trace row where your prediction and the branch disagree, and start there.
  • The original input got edited: use the reset below rather than trying to recreate it from memory.

The checker should accept the threshold and selected count. If your count differs, trace the first input line where the branch or count surprises you. The invalid-input run should fail at conversion, before the final summary.

Terminal window
cd ~/underdunn-course/labs/python-behavior
rm -rf -- python-behavior-v1.0.0
tar -xf ~/underdunn-course/downloads/python-behavior-v1.0.0.tar -C .

For final cleanup, run only the first two lines, and keep the verified archive and any working files you want.