
Let's use the free Open Meteo weather API (no key required).
import requests
def get_weather(latitude, longitude):
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": latitude,
"longitude": longitude,
"current_weather": True,
"temperature_unit": "fahrenheit"
}
try:
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
data = response.json()
weather = data["current_weather"]
return {
"temp": weather["temperature"],
"wind": weather["windspeed"],
"code": weather["weathercode"]
}
except requests.RequestException as e:
print(f"Error: {e}")
return None
result = get_weather(40.7128, -74.0060) # New York
if result:
print(f"Temperature: {result['temp']}°F")Patterns used: params dict for query strings, timeout, raise_for_status, try/except, nested JSON access.
Reference:
TaskLoco™ — The Sticky Note GOAT