File size: 1,114 Bytes
a2739ab
1eab79c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a84bdff
 
1eab79c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests

def get_weather_data(api_key, city):
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {"q": city, "appid": api_key, "units": "metric"}

    response = requests.get(base_url, params=params)

    if response.status_code == 200:
        return response.json()
    else:
        print("Failed to fetch weather data.")
        return None

def display_weather_info(weather_data):
    if weather_data:
        city = weather_data["name"]
        weather = weather_data["weather"][0]["description"]
        temperature = weather_data["main"]["temp"]
        wind_speed = weather_data["wind"]["speed"]

        print(f"Weather in {city}: {weather}")
        print(f"Temperature: {temperature}°C")
        print(f"Wind Speed: {wind_speed} m/s")
    else:
        print("Weather data is unavailable.")

def main():
    api_key = "1aafc3163909c1493596da9340e00aee"  # Replace with your OpenWeatherMap API key
    city = input("Enter a city name: ")

    weather_data = get_weather_data(api_key, city)
    display_weather_info(weather_data)

if __name__ == "__main__":
    main()