Weather app
import requests
def get_weather(city, api_key):
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": your-api_key-from-weather-api,
"units": "metric" # Use 'imperial' for Fahrenheit
}
try:
response = requests.get(base_url, params=params)
data = response.json()
if response.status_code == 200:
weather = data["weather"][0]["description"]
temp = data["main"]["temp"]
feels_like = data["main"]["feels_like"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
print(f"\nWeather in {city.title()}:")
print(f"- Condition: {weather}")
print(f"- Temperature: {temp}°C (feels like {feels_like}°C)")
print(f"- Humidity: {humidity}%")
print(f"- Wind Speed: {wind_speed} m/s")
else:
print(f"Error: {data.get('message', 'Unable to fetch weather.')}")
except Exception as e:
print(f"An error occurred: {e}")
def main():
print("--- Weather App ---")
city = input("Enter city name: ")
api_key = input("Enter your OpenWeatherMap API key: ")
get_weather(city, api_key)
if __name__ == "__main__":
main()
Code output
--- Weather App ---
Enter city name: Delhi
Enter your OpenWeatherMap API key: YOUR_API_KEY_HERE
Weather in Delhi:
- Condition: clear sky
- Temperature: 33°C (feels like 36°C)
- Humidity: 40%
- Wind Speed: 3.2 m/s