Exercise 2 solution: extending the pairing checker with mood and role ===================================================================== A sample answer (yours may differ; the mood tags are a matter of opinion). py pair2.py # name: (class, x-height fraction, mood, suited roles) FACES = { "Georgia": ("serif", 0.48, "friendly", {"heading", "body"}), "Times New Roman": ("serif", 0.45, "formal", {"heading", "body"}), "Verdana": ("sans-serif", 0.55, "friendly", {"body", "heading"}), "Arial": ("sans-serif", 0.52, "neutral", {"body", "heading"}), "Courier New": ("monospace", 0.42, "technical", {"code"}), "Impact": ("display", 0.50, "loud", {"heading"}), } # (heading face, body face) PAIRS = [("Georgia", "Verdana"), ("Georgia", "Times New Roman"), ("Arial", "Verdana"), ("Impact", "Georgia"), ("Georgia", "Impact"), ("Times New Roman", "Arial")] passed = 0 for head, body in PAIRS: ch, xh, mh, rh = FACES[head] cb, xb, mb, rb = FACES[body] warnings = [] if ch == cb and mh == mb: warnings.append("same class and mood") if "heading" not in rh: warnings.append(f"{head} not suited to heading") if "body" not in rb: warnings.append(f"{body} not suited to body") if abs(xh - xb) > 0.08: warnings.append("x-heights far apart") label = f"{head} / {body}" if warnings: print(f"{label:<30} WARN: " + "; ".join(warnings)) else: passed += 1 print(f"{label:<30} ok") print(f"\n{passed} of {len(PAIRS)} pairs passed without a warning") Output when run: Georgia / Verdana ok Georgia / Times New Roman ok Arial / Verdana ok Impact / Georgia ok Georgia / Impact WARN: Impact not suited to body Times New Roman / Arial ok 5 of 6 pairs passed without a warning What to notice -------------- * The role rule catches Impact as body text, exactly as intended. * The checker is too kind to Georgia / Times New Roman. Both are serifs, but the moods were tagged "friendly" and "formal", so the "same class AND same mood" rule did not fire. That is a weakness of the rule, not proof the pairing is good: two serifs this close in look still clash. A stricter version would warn whenever the class is the same and the x-height gap is under about 0.05. Try it, and see Chapter 5's first checker, which did exactly that. * Mood tags are opinions. Two people may tag the same face differently, so treat the output as a prompt for a second look, never as a verdict. * The "passed" count is only as good as the rules and the data. Looking at real text in the two faces is still the final test.