Categorizing blood pressure
Use integer readings in millimeters of mercury (mm Hg).
Write a function that takes positive systolic and diastolic blood pressure readings as input and returns a category (Normal, Elevated, Hypertension Stage 1, Hypertension Stage 2, or Severe Hypertension) using if/elif/else statements. Apply the categories from highest to lowest and use the more severe category when either reading crosses its threshold. Raise ValueError if either reading is not positive.
| Blood Pressure Category | Systolic Range | Diastolic Range |
|---|---|---|
| Normal | Below 120 | and below 80 |
| Elevated | 120–129 | and below 80 |
| Hypertension Stage 1 | 130–139 | or 80–89 |
| Hypertension Stage 2 | 140 and above | or 90 and above |
| Severe Hypertension | Above 180 | and/or above 120 |
Complete Listing 1. Save the completed code as categorize_blood_pressure.py and run it to check the supplied assertions.
def categorize_blood_pressure(systolic: int, diastolic: int) -> str:
raise NotImplementedError("Replace this line with your code.")
# Inside a band
got = categorize_blood_pressure(110, 70)
assert got == "Normal", f"110/70: expected 'Normal', got {got!r}"
# At the bottom of the elevated band, and just below it
got = categorize_blood_pressure(120, 75)
assert got == "Elevated", f"120/75: expected 'Elevated', got {got!r}"
got = categorize_blood_pressure(119, 79)
assert got == "Normal", f"119/79: expected 'Normal', got {got!r}"
# The systolic reading is elevated, but the diastolic one is higher in rank
got = categorize_blood_pressure(125, 85)
assert got == "Hypertension Stage 1", f"125/85: expected 'Hypertension Stage 1', got {got!r}"
# Both readings sit in stage 2
got = categorize_blood_pressure(150, 95)
assert got == "Hypertension Stage 2", f"150/95: expected 'Hypertension Stage 2', got {got!r}"
# Above every threshold
got = categorize_blood_pressure(190, 130)
assert got == "Severe Hypertension", f"190/130: expected 'Severe Hypertension', got {got!r}"
# A reading that is not positive is invalid input, not a category
try:
categorize_blood_pressure(0, 80)
raise AssertionError("0/80: expected ValueError, but a category was returned")
except ValueError:
pass
# Your case: which pair of readings is most likely to break it?
# got = categorize_blood_pressure(___, ___)
# assert got == "___", f"___: expected '___', got {got!r}"
TipHint
assert conditionchecks a Boolean condition. A true condition produces no output; a false condition raisesAssertionError. For example,assert 2 + 3 == 5passes.