Python Project - Create a Weather Application
Weather Application:
Create a program that fetches and displays weather information for a given location.
Input values:
User provides the location (e.g., city name, zip code) for which weather information is requested.
Output value:
Visual representation of the weather information fetched for the given location, including temperature, humidity, wind speed, etc.
Example:
Input values: Location: New York City Output value: Visual representation of New York City weather information: - Temperature: 55°F - Humidity: 70% - Wind speed: 10 mph - Weather Conditions: Cloudy Input values: Location: London Output value: Visual representation of weather information for London: - Temperature: 12°C - Humidity: 80% - Wind speed: 15 km/h - Weather Conditions: Rainy Input values: Location: Tokyo Output value: Visual representation of weather information for Tokyo: - Temperature: 20°C - Humidity: 60% - Wind speed: 8 km/h - Weather conditions: Sunny
OpenWeather presents you a comprehensive weather solution to get accurate and industry-specific weather forecasts, timely severe weather alerts, detailed weather overview, and expert consultations from OpenWeather professional meteorologists for informed decision-making, worldwide. All you need is to register and get an API key to make requests from OpenWeather.
Solution 1: Weather Application Using Requests and OpenWeatherMap API
Code:
import requests
# Function to fetch weather data from OpenWeatherMap API
def get_weather(city_name, api_key):
base_url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}"
# Make the request to the API
response = requests.get(base_url)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
main = data['main']
wind = data['wind']
weather = data['weather'][0]
# Prepare the weather data
weather_info = {
'city': city_name,
'temperature': main['temp'],
'humidity': main['humidity'],
'wind_speed': wind['speed'],
'conditions': weather['description']
}
return weather_info
else:
return None
# Main code block
if __name__ == "__main__":
api_key = '******************************' # Replace with your actual OpenWeatherMap API key
city_name = input("Enter city name: ")
# Fetch weather data
weather_data = get_weather(city_name, api_key)
# Display the results
if weather_data:
print(f"Weather in {weather_data['city']}:")
print(f"Temperature: {weather_data['temperature']}°C")
print(f"Humidity: {weather_data['humidity']}%")
print(f"Wind Speed: {weather_data['wind_speed']} m/s")
print(f"Conditions: {weather_data['conditions']}")
else:
print("City not found or API error.")
Output:
Enter city name: London Weather in London: Temperature: 287.6°C Humidity: 91% Wind Speed: 2.68 m/s Conditions: broken clouds
Enter city name: Paris Weather in Paris: Temperature: 290.19°C Humidity: 95% Wind Speed: 3.09 m/s Conditions: light intensity shower rain
Enter city name: New York Weather in New York: Temperature: 279.66°C Humidity: 69% Wind Speed: 3.6 m/s Conditions: clear sky
Explanation:
- OpenWeatherMap API: Uses the requests library to fetch weather data from OpenWeatherMap.
- API Key: Requires an API key for accessing weather data.
- Input and Output: Takes a city name as input and displays temperature, humidity, wind speed, and weather conditions.
- Error Handling: Handles errors for invalid city names or API issues.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/python/python-project-create-a-weather-application.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics