← Back

Program 11: Implement a basic rule-based expert system for weather classification.

Simple Python Code
print("=== Weather Expert System ===\n")

while True:
    # Get weather data from user
    temp = float(input("Enter temperature (°C): "))
    humidity = float(input("Enter humidity (%): "))
    
    # Classify weather based on rules
    if temp > 25 and humidity > 60:
        weather = "Hot and Humid"
    elif 15 <= temp <= 25 and 40 <= humidity <= 70:
        weather = "Pleasant"
    elif temp < 15 and humidity < 60:
        weather = "Cold"
    elif temp < 10 and humidity > 70:
        weather = "Cold and Wet"
    else:
        weather = "Unknown"
    
    # Display result
    print(f"\nWeather: {weather}\n")
    
    # Ask user to continue
    cont = input("Continue? (yes/no): ").lower()
    if cont != "yes":
        print("Weather System shutting down.")
        break
Advanced Python Code
class WeatherExpertSystem:
    def __init__(self):
        self.rules = [
            {'temp': (25, 35), 'humidity': (60, 100), 'weather': 'Hot and Humid'},
            {'temp': (15, 25), 'humidity': (40, 70), 'weather': 'Pleasant'},
            {'temp': (0, 15), 'humidity': (30, 60), 'weather': 'Cold'},
            {'temp': (-10, 10), 'humidity': (70, 100), 'weather': 'Cold and Wet'},
        ]
    
    def classify(self, temp, humidity):
        for rule in self.rules:
            if (rule['temp'][0] <= temp <= rule['temp'][1] and
                rule['humidity'][0] <= humidity <= rule['humidity'][1]):
                return rule['weather']
        return 'Unknown'

print("Weather Expert System:")

while True:
    weather_system = WeatherExpertSystem()

    temp = float(input("Enter temperature (°C): "))
    humidity = float(input("Enter humidity (%): "))

    classification = weather_system.classify(temp, humidity)
    print(f"Temperature: {temp}°C, Humidity: {humidity}%")
    print(f"Weather Classification: {classification}\n")

    cont = input("Continue? (yes/no): ").lower()
    if cont != "yes":
        print("Weather System shutting down.")
        break
Infographics