──────────────────────────────────────

11. Measure and Plan with Numbers · Check a Measured Range

Use an inclusive range to decide whether a numeric reading can be used.

Lesson files · lessons/numeric-tools/guard-a-measurement

Before you start
- Read a decimal measurement with float(input()).
- Use if and else to handle two outcomes.

By the end
- Use a chained comparison to check both ends of a range.
- Test an exact endpoint and a reading outside the range.

Where you can use it
Check a game’s volume setting before using it. Try both ends of the allowed range and a setting just outside it.

A grow light's timer accepts settings from 2 to 8 hours, including the two ends. In 2 <= hours <= 8, the first comparison checks that the reading is at least 2. The second checks that it is at most 8. Both need to pass.

Trace the two comparisons for 2, 8 and 8.1. Use the range check after converting the input to a number. A later lesson handles replies that fail conversion, such as the word eight.

Predict
What does the timer program print at exactly 8 hours?
- Timer ready
- Check the setting

Worked example (example.py)

hours = float(input())
if 2 <= hours <= 8:
    print("Timer ready")
else:
    print("Check the setting")


Example input
8

Example result
Timer ready

Read a jug’s volume in litres as one decimal number. Print In range for readings from 0 to 2 litres inclusive. For readings outside that range, print Out of range.

Starter program (starter.py)

litres = float(input())
# Decide whether the reading fits the marked range.
print("In range")


Starter input
2.01

Hint
Give the if a lower and an upper comparison. Use else for readings that fail either comparison.

Case 1. Try the sample
Input
2.01
Expected output
Out of range

Case 2. Exactly full
Input
2
Expected output
In range

Case 3. Negative reading
Input
-0.01
Expected output
Out of range

Stretch
Try a reading of zero. Then add an elif to give different messages for a negative reading and a reading above 2.

Reflect
Which comparison rules out a negative reading?

Check your prediction
Timer ready The second <= allows a reading equal to 8.
