Temperature with a Guarded Setter

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.5
Build Thermostat exposing celsius as a property whose setter rejects values below absolute zero (-273.15) with ValueError, and a derived read-only property fahrenheit. Property setters let you keep attribute syntax (t.celsius = 25) while still validating.

Examples

Example 1
Input
t = Thermostat(20); t.celsius = 25; t.fahrenheit
Output
77.0
Explanation

The setter accepted 25; fahrenheit derives from it.

Example 2
Input
t.celsius = -300
Output
ValueError
Explanation

Below absolute zero.

Constraints

  • celsius has @property and @celsius.setter.
  • fahrenheit has no setter.

Topics

OOPproperty setter

Companies

HoneywellBoschSiemens

Hints

Hint 1

Store the real value in self._celsius.

Hint 2

F = C × 9/5 + 32.

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