This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathTASK-5.py
More file actions
65 lines (54 loc) · 2.32 KB
/
Copy pathTASK-5.py
File metadata and controls
65 lines (54 loc) · 2.32 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import datetime
# Your OpenWeatherMap API key
API_KEY = '69e7dd8a8069d4066de2a18ea5996e36'
BASE_URL = 'http://api.openweathermap.org/data/2.5/'
# Function to get current weather data
def get_current_weather(city):
url = f"{BASE_URL}weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
return response.json()
# Function to get weather forecast data
def get_forecast(city):
url = f"{BASE_URL}forecast?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
return response.json()
# Function to display weather data
def display_weather_data(city):
current_weather = get_current_weather(city)
forecast = get_forecast(city)
if current_weather.get("cod") != 200 or forecast.get("cod") != "200":
print("Failed to retrieve data. Please check the city name or API key.")
return
# Current weather
print(f"\nCurrent weather in {city.capitalize()}:")
print(f"Temperature: {current_weather['main']['temp']}°C")
print(f"Weather: {current_weather['weather'][0]['description']}")
print(f"Humidity: {current_weather['main']['humidity']}%")
print(f"Wind Speed: {current_weather['wind']['speed']} m/s")
# Forecast
print(f"\n5-Day Forecast for {city.capitalize()}:")
for item in forecast['list']:
timestamp = item['dt']
date_time = datetime.datetime.fromtimestamp(timestamp)
if date_time.hour == 12: # Show data for 12 PM each day
temp = item['main']['temp']
weather = item['weather'][0]['description']
print(f"{date_time.strftime('%Y-%m-%d %H:%M:%S')}: {temp}°C, {weather}")
# Temperature trends (Average temperatures per day)
temp_trends = {}
for item in forecast['list']:
date = datetime.datetime.fromtimestamp(item['dt']).date()
if date not in temp_trends:
temp_trends[date] = []
temp_trends[date].append(item['main']['temp'])
print(f"\nTemperature Trends in {city.capitalize()}:")
for date, temps in temp_trends.items():
avg_temp = sum(temps) / len(temps)
print(f"{date}: {avg_temp:.2f}°C")
# Main function
def main():
city = input("Enter the city name: ")
display_weather_data(city)
if __name__ == "__main__":
main()