Weather Forecast System


Write a Python class WeatherForecast that stores daily weather data (temperature and rainfall) and has methods to calculate the average temperature and total rainfall for a given period. The class should provide methods to add daily weather data.

Example 1:

Input: add_data(25, 10), add_data(28, 5), average_temperature()
Output: 26.5

Example 2:

Input: add_data(25, 10), add_data(28, 5), total_rainfall()
Output: 15

Create methods to add data, calculate average temperature and total rainfall.

class WeatherForecast:
    def __init__(self):
        self.temperatures = []
        self.rainfalls = []
    def add_data(self, temperature, rainfall):

    def average_temperature(self):
        return sum(self.temperatures) / len(self.temperatures)

    def total_rainfall(self):
        return sum(self.rainfalls)

# Example usage
forecast = WeatherForecast()
forecast.add_data(25, 10)
forecast.add_data(28, 5)
print(forecast.average_temperature())  # Output: 26.5
print(forecast.total_rainfall())  # Output: 15

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!