Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Sync Related Attributes via a Property Setter

Use property getters/setters to keep related attributes consistent (Celsius ↔ Fahrenheit) with validation.

Python practice15 minEncapsulation & Access PatternsIntermediateLast updated April 8, 2026

Problem statement

Create a Temperature class that stores a temperature internally in Celsius but exposes both celsius and fahrenheit as properties. Setting either property should update the other so they remain consistent. The class should: - Allow construction via either celsius or fahrenheit keyword arguments (or none, default to 0°C). - Expose .celsius and .fahrenheit properties with both getter and setter. - Validate that temperatures are not below absolute zero (-273.15°C); if a setter receives a value below absolute zero, raise a ValueError with a helpful message. - If both celsius and fahrenheit are provided to the constructor, prefer the celsius argument and compute fahrenheit from that. This exercise focuses on encapsulation: store the internal state in a private attribute (e.g., _celsius) and use property logic to keep both views consistent.

Task

Implement a Temperature class where celsius and fahrenheit are kept in sync using property setters, including validation against absolute zero.

Examples

Construct with Celsius and read Fahrenheit

Input

Temperature(celsius=100).fahrenheit

Output

212.0

100°C is 212°F.

Input format

This problem expects you to implement a Temperature class. The tests will create Temperature instances and access their .celsius and .fahrenheit properties.

Output format

The properties should return numeric values (floats).

Constraints

- Use a private attribute to store the value in Celsius (e.g., self._celsius). - Implement validation to prevent values below -273.15°C (raise ValueError). - Constructor signature: def __init__(self, celsius: float = None, fahrenheit: float = None) - No external libraries.

Samples

Sample 1

Input

Temperature(fahrenheit=32).celsius

Output

0.0

32°F equals 0°C; the property getter converts correctly.