import datetime

# This function helps us make a smart guess about tomorrow's temperature
def predict_temperature(past_temperatures, weights):
    """
    Calculate the weighted average prediction for temperature.
    
    :param past_temperatures: List of (date, temperature) pairs
    :param weights: List of how important each temperature is
    :return: Our best guess for tomorrow's temperature
    """
    total_weighted_temp = 0
    total_weight = 0
    
    # We look at each past temperature and how important it is
    for (date, temp), weight in zip(past_temperatures, weights):
        # We multiply each temperature by how important it is
        # This helps us pay more attention to more important temperatures
        total_weighted_temp += temp * weight
        # We keep track of the total importance
        total_weight += weight
    
    # We divide the total by the total importance
    # This gives us our final guess for tomorrow's temperature
    return total_weighted_temp / total_weight if total_weight != 0 else 0

# Here's the temperature information we have from the past few days
past_temperatures = [
    (datetime.datetime(2024, 7, 28), 75),  # July 28th temperature
    (datetime.datetime(2024, 7, 29), 78),  # July 29th temperature
    (datetime.datetime(2024, 7, 30), 80),  # July 30th temperature (today)
]

# We decide how important each day's temperature is
# Today is July 30th, so we'll use that as our reference point
today = datetime.datetime(2024, 7, 30)

# We make newer temperatures more important than older ones
# We do this by dividing 1 by the number of days ago the temperature was recorded
# This means today's temperature (0 days ago) is the most important
weights = []

# We look at each past temperature one by one
for date, temperature in past_temperatures:
    # We figure out how many days ago this temperature was recorded
    days_ago = (today - date).days
    
    # If it's today's temperature, we need to handle it specially
    if days_ago == 0:
        # Today's temperature is the most important, so we give it a weight of 1
        importance = 1
    else:
        # For past days, we make them less important by dividing 1 by the number of days ago
        # This means more recent days are more important
        importance = 1 / days_ago
    
    # We add this importance to our list of weights
    weights.append(importance)

# Now we use our function to guess tomorrow's temperature
prediction = predict_temperature(past_temperatures, weights)

# We show our guess for tomorrow's temperature
print(f"Our best guess for tomorrow's temperature is: {prediction:.1f}°F")

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: