CCCPaste

Python I/O File Debugging

#-------------------------app.py--------------------

import csv

def process_weather_data(filename):
    """
    Processes weather data from a CSV file.
    
    Args:
        filename (str): The name of the CSV file.
    
    Returns:
        A dictionary containing processed data or None if an error occurs.
    """
    
    # Initialize variables to store results
    total_temp = 0
    record_count = 0
    highest_temp = -float('inf')
    lowest_temp = float('inf')
    highest_temp_date = ""
    lowest_temp_date = ""
    
    try:
        with open(filename, mode='r') as file:
            reader = csv.DictReader(file)
            for row in reader:
                # Student's task: Add code here to process each row
                pass
                
        # Student's task: Add code here to calculate the average temperature
        
        return {
            'average_temp': 0, # Update this with the calculated average
            'highest_temp': highest_temp,
            'highest_temp_date': highest_temp_date,
            'lowest_temp': lowest_temp,
            'lowest_temp_date': lowest_temp_date
        }

    except FileNotFoundError:
        print(f"Error: The file '{filename}' was not found.")
        return None
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return None

# Main part of the script
if __name__ == "__main__":
    result = process_weather_data('weather_data.csv')
    if result:
        print("Weather Data Analysis:")
        print(f"Average Temperature: {result['average_temp']:.2f}°F")
        print(f"Highest Temperature: {result['highest_temp']}°F (on {result['highest_temp_date']})")
        print(f"Lowest Temperature: {result['lowest_temp']}°F (on {result['lowest_temp_date']})")

#--------------------------weather_data.csv--------------------
date,temperature_f
2023-01-01,35
2023-01-02,42
2023-01-03,45
2023-01-04,38
2023-01-05,29
2023-01-06,31
2023-01-07,48
2023-01-08,55
2023-01-09,49
2023-01-10,40