Monday, June 17, 2024

"Hey Google! What's the temperature?"

Thermometer

To create a simple script to check the weather in your specific city, you can use a programming language like Python and an API (Application Programming Interface) that provides weather data. For this example, I'll use the OpenWeatherMap API, which offers free weather data. You'll need to sign up for a free API key from their website located at (https://openweathermap.org/api) to use their service.

Here's a Python script to check the weather in your specific city:

import requests

def get_weather(api_key, city_name):
    base_url = "http://api.openweathermap.org/data/2.5/weather?"
    complete_url = f"{base_url}q={city_name}&appid={api_key}&units=metric"

    response = requests.get(complete_url)
    data = response.json()

    if data["cod"] == "404":
        print("City not found!")
        return

    weather_description = data["weather"][0]["description"]
    temperature = data["main"]["temp"]
    humidity = data["main"]["humidity"]
    wind_speed = data["wind"]["speed"]
    
    print(f"Weather in {city_name}:")
    print(f"Description: {weather_description}")
    print(f"Temperature: {temperature}°C")
    print(f"Humidity: {humidity}%")
    print(f"Wind Speed: {wind_speed} m/s")

if __name__ == "__main__":
    # Replace 'YOUR_API_KEY' with your actual API key from OpenWeatherMap
    api_key = "YOUR_API_KEY"
    city_name = input("Enter your city name: ")
    get_weather(api_key, city_name)

Before running the script, make sure you have Python installed on your computer. Copy the code above into a Python file (e.g., weather.py), replace 'YOUR_API_KEY' with your actual API key from OpenWeatherMap, and save the file.

When you run the script, it will prompt you to enter your city name. After providing the city name, the script will fetch and display the current weather information for that city, including the weather description, temperature, humidity, and wind speed.

Please note that this is a basic example, and you can enhance the script further by adding error handling, additional weather data, or displaying forecasts for multiple days.

Source: Some or all of the content was generated using an AI language model

No comments:

Post a Comment

Contact The Wizard!
(he/him)