It is the last build day. Every part is ready. Rocket's finger hovers over the run button.
"What if something breaks?" he whispers.
Raven points at the test list. "What do you notice we wrote first, last week?"
"The tests!" Rocket says. "If they all pass, we know each part works."
Nova glows her brightest. "Here is a hint. Run the tests first. Then run the report."
# Part 6: tests
def check(label, got, want):
if got == ____:
print("PASS", label)
else:
print("FAIL", label, "got", got, "want", want)
def run_tests():
check("total", total_items({"a": 2, "b": 3}), 5)
check("total of nothing", total_items({}), 0)
check("low", low_stock({"b": 9, "c": 0, "a": 1}, 5), ["a", "c"])
check("find", find_item(["lamps", "maps", "rope"], "maps"), 1)
check("find missing", find_item(["lamps", "maps"], "tea"), -1)
check("crew", count_crew(fleet), 8)
# Part 7: the report
def report(stock, tree):
crew = ____(tree)
print("Crew:", crew)
print("Items:", total_items(stock))
print("Water each:", stock["water"] // crew)
print("Low:", ", ".join(low_stock(stock, 5)))
names = sorted(stock)
print("maps is at spot", find_item(names, "maps"))
run_tests()
restock(inventory, "rope", 10)
restock(inventory, "kites", 4)
report(inventory, fleet) All seven parts together make one 96-line program. Here is what it prints when it runs.
Output: PASS total PASS total of nothing PASS low PASS find PASS find missing PASS crew Crew: 8 Items: 93 Water each: 5 Low: kites, lamps maps is at spot 2
def check(label, got, want):
if got == want:
print("PASS", label)
else:
print("FAIL", label, "got", got, "want", want)
def total_items(stock):
total = 0
for item in stock:
total = total + stock[item]
return total
check("two", total_items({"a": 1, "b": 1}), 2)
check("one", total_items({"a": 4}), 5) | Statement | True or false? |
|---|---|
| The test labelled two passes. | ? |
| A FAIL always means the code has a bug. | ? |
| check prints the got and want values on a FAIL. | ? |
| All six tracker tests pass in the full program. | ? |
You shipped it! A 96-line program, built one tested part at a time. Tomorrow is graduation.