In-Range Check With Chained Comparisons

Medium ⏱ 12 min 51% acceptance ★★★★★ 4.8
Write a function temperature_status(temp) that classifies a Celsius reading using Python's chained comparison syntax: "Freezing" if temp <= 0, "Cold" if 0 < temp <= 15, "Mild" if 15 < temp <= 25, "Hot" if temp > 25. You must use chained comparisons (e.g. 15 < temp <= 25) rather than combining two separate comparisons with and.

Examples

Example 1
Input
temperature_status(20)
Output
"Mild"
Explanation

15 < 20 <= 25.

Example 2
Input
temperature_status(-3)
Output
"Freezing"
Explanation

temp is <= 0.

Constraints

  • temp is an integer or float.

Topics

Conditional Statementschained comparisons

Companies

GoogleMeta

Hints

Hint 1

Python allows writing `a < x <= b` directly as one expression.

Hint 2

Order the branches from lowest to highest for clarity.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.