Weather

import requests import json def get_weather(city): api_key = "YOUR_API_KEY" # Replace with your actual API key base_url = "http://api.openweathermap.org/data/2.5/weather" params = { "q": city, "appid": api_key, "units": "metric" } response = requests.get(base_url, params=params) data = json.loads(response.text) if data["cod"] != "404": main_info = data["weather"][0]["main"] description = data["weather"][0]["description"] temperature = data["main"]["temp"] humidity = data["main"]["humidity"] wind_speed = data["wind"]["speed"] print(f"Weather in {city}:") print(f"Main: {main_info}") print(f"Description: {description}") print(f"Temperature: {temperature}°C") print(f"Humidity: {humidity}%") print(f"Wind Speed: {wind_speed} m/s") else: print("City not found.") city = input("Enter city name: ") get_weather(city)

Comments

Popular Posts