Exercise 2: An Invalid z-index Falls Back to 0 — Possible Solution ==================================================================== THE TEST ------------------------------ box_with_auto = block({'z-index': 'auto'}) get_z_index(box_with_auto) RESULT ------------------------------ 0 No error is raised, despite 'auto' being a real, valid CSS keyword that this course's own simplified z-index model never explicitly handles as a special case. WHICH PART OF THE FUNCTION IS RESPONSIBLE ------------------------------ def get_z_index(layout_box): style = box_style(layout_box) z = style.get('z-index', '0') try: return int(float(z)) except (ValueError, TypeError): return 0 The try/except block is responsible. `float('auto')` is not a mistake Python can recover from silently -- 'auto' is not a valid numeric string in any sense, so float() raises ValueError: could not convert string to float: 'auto'. Because that call sits inside a try block, the except clause catches it (ValueError is explicitly listed) and returns 0 instead of letting the exception propagate out of get_z_index entirely. WHY THIS IS A REASONABLE FALLBACK, NOT JUST A CRASH-AVOIDANCE HACK ------------------------------ Real CSS's own actual initial value for z-index IS the keyword 'auto' -- and 'auto' behaves, for stacking-order comparison purposes, essentially like 0 (it participates in painting at the same "level" as z-index:0 content, the key difference being whether it creates a NEW stacking context at all, a distinction this chapter's own scoped-down model doesn't attempt to implement). So falling back to 0 for 'auto' isn't merely "avoid crashing" -- it happens to land on the right numeric behavior for sibling-order comparison purposes too, even though the fallback path was written generically (to catch ANY unparseable value, not specifically to special-case the string 'auto'). WHY THIS WORKS AS AN ANSWER ------------------------------ This is the same defensive-lookup pattern this course has used repeatedly -- Chapter 6's DEFAULT_CHAR_WIDTH_EM fallback, Chapter 8's DEFAULT_COLOR fallback -- catching an entire CLASS of unexpected input (any string that isn't a clean integer-like number) with one generic handler, rather than trying to enumerate every specific CSS keyword that might appear in a z-index declaration (auto, inherit, initial, unset, and so on all being real, valid CSS values this simplified engine was never going to model individually).