Advanced OOP and patterns · 6 / 15 Exercise Medium

Temperature with properties

Objective

A property (@property) looks like an attribute but runs code when read or assigned: it is used to validate and to expose derived values. Here a class stores the temperature in Celsius and offers Fahrenheit as a computed property that can also be assigned.

Instructions

  • Create the class Temperature with the properties celsius (it cannot go below -273.15: otherwise it raises ValueError) and fahrenheit (computed from Celsius; when assigned it updates Celsius).
  • The first line has the number n of commands; each one is C value or F value. After each command show x C = y F with one decimal, or Impossible temperature if the value is impossible (and the state does not change).
Input
3
C 100
F 32
C -300
Output
100.0 C = 212.0 F
0.0 C = 32.0 F
Impossible temperature

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise