-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClima.py
More file actions
17 lines (16 loc) · 751 Bytes
/
Copy pathClima.py
File metadata and controls
17 lines (16 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import requests
API_KEY = "TU_API_KEY" # Consigue una API Key en https://openweathermap.org/api
URL_BASE = "http://api.openweathermap.org/data/2.5/weather"
def obtener_clima(ciudad):
params = {"q": ciudad, "appid": API_KEY, "units": "metric", "lang": "es"}
respuesta = requests.get(URL_BASE, params=params)
if respuesta.status_code == 200:
datos = respuesta.json()
temp = datos["main"]["temp"]
descripcion = datos["weather"][0]["description"]
print(f"El clima en {ciudad} es: {descripcion}, con {temp}°C.")
else:
print("No se pudo obtener el clima. Revisa la ciudad o la API Key.")
if __name__ == "__main__":
ciudad = input("Introduce el nombre de la ciudad: ")
obtener_clima(ciudad)