Exerciseschevron_rightChapter 3chevron_right3.2
fitness_center

Exercise 3.2

Well Header Builder

Level 2
Chapter 3: Data Structures
descriptionProblem

A well header is the standard summary record that travels with every well: name, field, operator, depths, well type, status. Different operators use slightly different fields, but the structure is the same everywhere; and so is the validation discipline.

Write build_well_header(**kwargs) that returns a dictionary representing a well header. Required keys:

  • name, field, operator, total_depth, well_type, status

Validation rules (raise ValueError on any violation):

  • total_depth must be a positive number.
  • well_type must be one of "Exploration", "Appraisal",

"Development".

  • status must be one of "Drilling", "Completing", "Producing",

"Shut-in", "Abandoned".

On success, return the kwargs as a dict (you may include additional keys the caller passed). Use **kwargs so the function picks up fields the spec adds later without your code having to change.

lightbulbHints (0/3)

Stuck? Reveal hints one at a time — they progress from nudge to near-solution.

codeYour solution
main.py
visibilityReveal reference solutionexpand_more

Try solving it yourself first — the hints walk you through it. The solution below is one valid approach; yours may differ and still be correct.

REQUIRED   = ("name", "field", "operator", "total_depth", "well_type", "status")
TYPES_OK   = {"Exploration", "Appraisal", "Development"}
STATUS_OK  = {"Drilling", "Completing", "Producing", "Shut-in", "Abandoned"}


def build_well_header(**kwargs):
    for key in REQUIRED:
        if key not in kwargs:
            raise ValueError(f"missing required field {key!r}")

    if not isinstance(kwargs["total_depth"], (int, float)) or kwargs["total_depth"] <= 0:
        raise ValueError(f"total_depth must be a positive number, got {kwargs['total_depth']!r}")

    if kwargs["well_type"] not in TYPES_OK:
        raise ValueError(f"well_type must be one of {sorted(TYPES_OK)}, got {kwargs['well_type']!r}")

    if kwargs["status"] not in STATUS_OK:
        raise ValueError(f"status must be one of {sorted(STATUS_OK)}, got {kwargs['status']!r}")

    return dict(kwargs)


h = build_well_header(
    name="OD-003",
    field="OML 58",
    operator="National Petroleum Development Co.",
    total_depth=12400,
    well_type="Development",
    status="Producing",
)
print(h)

lockCopying code is a Full Access feature.