This script uses the requests library to send a request to the OpenWeatherMap API and retrieve the weather data for the user’s location. It then extracts the weather data and prints it to the console along with the current time and temperature.
To use this script, you’ll need to sign up for a free API key from OpenWeatherMap. You can then replace YOUR_API_KEY in the code with your own API key.
import requests
import datetime
# Get the current time and date
now = datetime.datetime.now()
# Get the user's location
location = input("Enter your location: ")
# Use the OpenWeatherMap API to get the weather for the user's location
api_key = "go to api.openweathermap.org to get your API KEY"
api_url = "https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}"
response = requests.get(api_url)
data = response.json()
# Extract the weather data from the API response
weather = data["weather"][0]["main"]
temperature = data["main"]["temp"]
# Print the time, weather, and temperature to the console
print("It is currently {} and {} degrees in {}".format(now.strftime("%H:%M"), temperature, location))
print("The weather is:", weather)