Skip to content

Latest commit

History

History
112 lines (84 loc) 路 4.32 KB

Weather Application.md

File metadata and controls

112 lines (84 loc) 路 4.32 KB

Certainly! Here's the introduction for the "Weather Application" project:

Weather Application

The "Weather Application" is a Python program that allows users to retrieve real-time weather information for a specified city. With this project, you can enhance your Python skills while working with external APIs to fetch live weather data. The application provides data on temperature, humidity, weather description, and wind speed, making it a handy tool for checking the weather in different locations.

Requirements:

  • Python 3.x
  • Requests library (install using pip install requests)
  • An API key from OpenWeatherMap (replace the API key in the code with your own)

Why This Project

Why This Project: This project serves as a practical exercise for Python programmers to work with external APIs, parse JSON data, and provide real-world value. It offers a chance to explore real-time weather data and practice Python programming.

Key Features:

  1. Weather Information: Users can enter the name of a city, and the application fetches and displays the current weather data.
  2. Temperature in Celsius: The temperature is provided in Celsius, making it easy to understand for users worldwide.
  3. Humidity Percentage: Users can check the humidity level in the specified city.
  4. Weather Description: Get a brief description of the current weather conditions.
  5. Wind Speed: Find out the wind speed in meters per second for the selected location.

Project Structure

  • The program is organized into a Python class called WeatherApp.
  • It uses the requests library to fetch data from the OpenWeatherMap API.
  • Users interact with the application through a simple command-line interface.
  • An OpenWeatherMap API key is required for accessing weather data.

How to Use:

  1. Run the Python program.
  2. Enter the name of the city you want to check the weather for.
  3. The application will retrieve and display the real-time weather data for the city.
  4. To exit the program, type 'q' and press Enter.

Suggested Enhancements

  • Multiple Cities: Allow users to check the weather for multiple cities in a single run.
  • Forecast Data: Extend the application to provide weather forecasts for upcoming days.
  • Graphical Interface: Create a graphical user interface (GUI) for a more user-friendly experience.

And here's the code for the "Weather Application":

import requests
import json

class WeatherApp:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = "https://api.openweathermap.org/data/2.5/weather"

    def get_weather(self, city):
        try:
            params = {
                "q": city,
                "appid": self.api_key,
                "units": "metric"  # Celsius
            }

            response = requests.get(self.base_url, params=params)
            data = json.loads(response.text)

            if response.status_code == 200:
                temperature = data["main"]["temp"]
                humidity = data["main"]["humidity"]
                description = data["weather"][0]["description"]
                wind_speed = data["wind"]["speed"]

                print(f"Weather in {city}:")
                print(f"Temperature: {temperature}掳C")
                print(f"Humidity: {humidity}%")
                print(f"Description: {description}")
                print(f"Wind Speed: {wind_speed} m/s")
            else:
                print("Error: Unable to fetch weather data.")

        except Exception as e:
            print(f"An error occurred: {str(e)}")

def main():
    api_key = "you api key"  # Replace with your OpenWeatherMap API key
    weather_app = WeatherApp(api_key)

    while True:
        city = input("Enter city name (or 'q' to quit): ")
        if city.lower() == 'q':
            break

        weather_app.get_weather(city)

if __name__ == "__main__":
    main()

Example Output:

$ python weather_application.py
Enter city name (or 'q' to quit): Mersin
Weather in Mersin:
Temperature: 14.5掳C
Humidity: 63%
Description: broken clouds
Wind Speed: 1.72 m/s
Enter city name (or 'q' to quit): q

Process finished with exit code 0

Feel free to use and customize this "Weather Application" for your learning and practical use.