Python script that detects the weather forecast and temperature

Use various Python libraries like requests and json to fetch weather data from an API like OpenWeatherMap or Weatherstack. Here's a basic example using



OpenWeatherMap API:

import requests def get_weather(city): api_key = 'YOUR_API_KEY' url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric' response = requests.get(url) data = response.json() if response.status_code == 200: weather_desc = data['weather'][0]['description'] temperature = data['main']['temp'] return f"The weather in {city} is {weather_desc} with a temperature of {temperature}°C." else: return "Error fetching weather data." city = input("Enter city name: ") print(get_weather(city))


Replace 'YOUR_API_KEY' with your actual API key from OpenWeatherMap. This script prompts the user for a city name and then fetches the weather information for that city, displaying the weather description and temperature in Celsius.


Comments

Popular posts from this blog

Fetching Data from an API in React Native with Expo

State Management in React Native with Expo: A Beginner’s Guide

How to Set Up React Native with Expo 🚀