generated from mwc/lab_weather
	All of these systems use lists and dictionaries for keeping track of positional and symbolic arguments.
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.4 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. 
 | 
						|
    """
 | 
						|
    
 | 
						|
    coords = estimate_location() if location is None else geocode_location(location)
 | 
						|
    if not coords:
 | 
						|
        return print("Cannot determine location.")
 | 
						|
    
 | 
						|
    office = get_weather_office(**coords)
 | 
						|
    if not office:
 | 
						|
        return print("Cannot determine NWS office.")
 | 
						|
    
 | 
						|
    weather = get_forecast(**(office | {'metric': metric}))
 | 
						|
    if not weather:
 | 
						|
        return print("Forecast cannot be found.")
 | 
						|
    
 | 
						|
    print(f"Today will be {weather[0]["description"].lower()} with an average temperature of {weather[0]["temperature"]}°{"C" if metric else "F"}.")
 | 
						|
    if verbose:
 | 
						|
        print(f"Winds headed {weather[0]["wind_direction"]} at {weather[0]["wind_speed"]}.")
 | 
						|
        
 |