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
Temperaturewith the propertiescelsius(it cannot go below-273.15: otherwise it raisesValueError) andfahrenheit(computed from Celsius; when assigned it updates Celsius). - The first line has the number
nof commands; each one isC valueorF value. After each command showx C = y Fwith one decimal, orImpossible temperatureif the value is impossible (and the state does not change).
3
C 100
F 32
C -300
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