generated from mwc/lab_weather
	Functions, programs, and distributed services all take input, process it, and produce output, often using structured data like lists or dictionaries.
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# weather.py
 | 
						|
# ------------
 | 
						|
# By MWC Contributors
 | 
						|
#
 | 
						|
# Defines `print_weather`, which does all the work of fetching 
 | 
						|
# the requested weather data and printing it out to the screen
 | 
						|
# in a sensible way. 
 | 
						|
# 
 | 
						|
# It's your job to implement this function.
 | 
						|
 | 
						|
from weather.weather_apis import (
 | 
						|
    geocode_location,
 | 
						|
    estimate_location,
 | 
						|
    get_weather_office,
 | 
						|
    get_forecast
 | 
						|
)
 | 
						|
 | 
						|
def print_weather(location=None, metric=False, verbose=False):
 | 
						|
    """Prints out a weather report using the provided location, or using
 | 
						|
    the user's current location if no location was provided. 
 | 
						|
    When metric is True, prints out the weather in metric units.
 | 
						|
    When verbose is True, prints out a more detailed report. 
 | 
						|
    """
 | 
						|
    loc = geocode_location(location) if location else estimate_location()
 | 
						|
    office = get_weather_office(loc['lat'], loc['lng'])
 | 
						|
    if not office:
 | 
						|
        print("No weather station available for this location.")
 | 
						|
        return
 | 
						|
    forecast = get_forecast(office['office'], office['x'], office['y'])
 | 
						|
    if not forecast:
 | 
						|
        print("Weather data not available.")
 | 
						|
        return
 | 
						|
    first = forecast[0]
 | 
						|
    temp = first['temperature']
 | 
						|
    if metric:
 | 
						|
        temp = (temp - 32) * 5/9  # Convert Fahrenheit to Celsius
 | 
						|
    
 | 
						|
    print(f"Weather: {first['description']}")
 | 
						|
    print(f"Temperature: {temp:.1f}°{'C' if metric else 'F'}")
 | 
						|
    
 |