Weather Station

Implement a class Weather that holds private attributes for temperature and humidity. Include methods to update these values, ensuring that they are within reasonable limits.
Example 1:
Input: Update: Temperature: 25, Humidity: 50
Output: "Temperature: 25, Humidity: 50"
Example 2:
Input: Update: Temperature: -100, Humidity: 200
Output: "Invalid values"

Check the values before updating the temperature and humidity.

class Weather:
    def __init__(self, temperature=20, humidity=50):
        self._temperature = temperature
        self._humidity = humidity

    def update(self, temperature, humidity):
        if -50 <= temperature <= 50 and 0 <= humidity <= 100:
            self._temperature = temperature
            self._humidity = humidity
        else:
            return "Invalid values"

    def get_weather(self):
        return f"Temperature: {self._temperature}, Humidity: {self._humidity}"

# Test the class
weather = Weather()
weather.update(25, 50)
print(weather.get_weather())  # Output: Temperature: 25, Humidity: 50

weather.update(-100, 200)
print(weather.get_weather())  # Output: Invalid values

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!