Command Palette

Search for a command to run...

Hectal
Phase 8: Object-Oriented Python
Python · Exercise 3 of 6 PreviousNext
Intermediate

A validated temperature property

Write a class Temperature whose __init__(self, celsius) does self.celsius = celsius, where celsius is a property. The property keeps the real number in self._celsius.

The setter raises ValueError if the value is below -273.15, and otherwise stores it.

Add a read-only property fahrenheit that returns celsius * 9 / 5 + 32.

What Check tests

  1. Temperature(100).celsius is 100
  2. Temperature(100).fahrenheit is 212
  3. Creating one below absolute zero raises ValueError
  4. Exactly -273.15 is allowed
  5. fahrenheit follows later changes to celsius
  6. Assigning a bad value later also raises ValueError
  7. fahrenheit cannot be assigned

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.