Properties
Completed
Create Temperature storing Celsius in _celsius, with a celsius property whose setter raises ValueError("below absolute zero") under -273.15, and a read-only fahrenheit property. Read values; for each set the temperature and print Fahrenheit (1 decimal) or the error.
Input
Line 1: n. Then n numbers.
Output
n lines.
Example
Input
3 100 -300 0
Output
212.0 below absolute zero 32.0
Editor
Python
Hints
@property turns a method into an attribute you read without ().
@name.setter runs when you assign to it.
Solution explanation
Properties let you add validation without changing how callers use the attribute.
Show solution
class Temperature:
def __init__(self):
self._celsius = 0.0
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("below absolute zero")
self._celsius = value
@property
def fahrenheit(self):
return self._celsius * 9 / 5 + 32
t = Temperature()
for _ in range(int(input())):
try:
t.celsius = float(input())
print(f"{t.fahrenheit:.1f}")
except ValueError as err:
print(err)