File size: 943 Bytes
a2739ab
 
 
 
 
 
29c745c
 
 
 
a2739ab
29c745c
 
 
 
 
 
 
 
 
 
 
a2739ab
29c745c
 
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
import requests
import json

# Replace YOUR_API_KEY_HERE with your actual API key from OpenWeatherMap
api_key = "1aafc3163909c1493596da9340e00aee"

def get_weather(city_name):
    # Make the API call to get the current weather details
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}"
    response = requests.get(url)

    # Extract the relevant information from the response
    if response.status_code == 200:
        data = json.loads(response.content)
        temperature = data['main']['temp']
        description = data['weather'][0]['description']
        wind_speed = data['wind']['speed']
        print(f"Temperature: {temperature - 273.15:.1f} °C")
        print(f"Weather Description: {description.title()}")
        print(f"Wind Speed: {wind_speed} m/s")
    else:
        print(response.json()['message'])

# Call the get_weather() function with a city name argument
get_weather("London")