generated from mwc/lab_weather
i made it look better
This commit is contained in:
@@ -1,24 +1,74 @@
|
||||
# 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 (
|
||||
ffrom weather.weather_apis import (
|
||||
geocode_location,
|
||||
estimate_location,
|
||||
get_weather_office,
|
||||
get_forecast
|
||||
)
|
||||
|
||||
# -----------------------------------------------------------
|
||||
# Choose weather emoji based on the description
|
||||
# -----------------------------------------------------------
|
||||
def choose_emoji(description):
|
||||
desc = description.lower()
|
||||
|
||||
if "snow" in desc:
|
||||
return "❄️"
|
||||
if "rain" in desc or "shower" in desc:
|
||||
return "🌧️"
|
||||
if "thunder" in desc or "storm" in desc:
|
||||
return "⛈️"
|
||||
if "cloud" in desc:
|
||||
return "☁️"
|
||||
if "sunny" in desc or "clear" in desc:
|
||||
return "☀️"
|
||||
if "wind" in desc or "breeze" in desc:
|
||||
return "💨"
|
||||
if "fog" in desc or "mist" in desc:
|
||||
return "🌫️"
|
||||
|
||||
return "🌡️" # default
|
||||
|
||||
# -----------------------------------------------------------
|
||||
# Main weather print function
|
||||
# -----------------------------------------------------------
|
||||
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.
|
||||
"""
|
||||
print("Not finished...") # YOUR CODE HERE!
|
||||
|
||||
# Get coordinates
|
||||
if location:
|
||||
coordinates = geocode_location(location)
|
||||
else:
|
||||
coordinates = estimate_location()
|
||||
|
||||
# Get NOAA office + forecast
|
||||
office = get_weather_office(coordinates["lat"], coordinates["lng"])
|
||||
forecast = get_forecast(office["office"], office["x"], office["y"], metric=metric)
|
||||
|
||||
# Pretty output
|
||||
print("\n====== Weather Forecast ======")
|
||||
print(f"Location: {location if location else 'Your Current Location'}")
|
||||
print(f"Office: {office['office']}")
|
||||
print("=================================\n")
|
||||
|
||||
if not forecast:
|
||||
print("No forecast available.")
|
||||
return
|
||||
|
||||
# Print only TODAY
|
||||
today = forecast[0]
|
||||
emoji = choose_emoji(today["description"])
|
||||
|
||||
print(f"{emoji} --- {today['name']} --- {emoji}")
|
||||
print(f"{emoji} {today['description']}")
|
||||
print(f"🌡️ Temperature: {today['temperature']}°{'C' if metric else 'F'}")
|
||||
print(f"💨 Wind: {today['wind_speed']} from {today['wind_direction']}")
|
||||
|
||||
if verbose:
|
||||
print("\nAdditional Details:")
|
||||
for key, value in today.items():
|
||||
if key not in ("name", "description", "temperature", "wind_speed", "wind_direction"):
|
||||
print(f" - {key}: {value}")
|
||||
Reference in New Issue
Block a user