-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
42 lines (34 loc) · 1.13 KB
/
code
File metadata and controls
42 lines (34 loc) · 1.13 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
import requests
API_KEY = "5636da6712032f442840ed113b0a9b86"
def get_weather(city):
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
'q': city.strip(),
'appid': API_KEY,
'units': 'metric'
}
try:
response = requests.get(base_url, params=params)
print("API Raw Response:", response.text) # Debug line
response.raise_for_status()
data = response.json()
city_name = data['name']
temp = data['main']['temp']
description = data['weather'][0]['description'].capitalize()
humidity = data['main']['humidity']
wind = data['wind']['speed']
result = (
f"\nCity: {city_name}\n"
f"Temperature: {temp}°C\n"
f"Condition: {description}\n"
f"Humidity: {humidity}%\n"
f"Wind Speed: {wind} m/s"
)
return result
except requests.exceptions.HTTPError as http_err:
return f"HTTP Error: {http_err}"
except Exception as e:
return f"Error: {str(e)}"
# Ask for city name
city = input("Enter city name: ")
print(get_weather(city))