forked from CloudLabsAI-Azure/Code-Generation-Refactoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_script.py
More file actions
27 lines (25 loc) · 844 Bytes
/
weather_script.py
File metadata and controls
27 lines (25 loc) · 844 Bytes
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
import requests
def get_weather(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:
data = response.json()
main = data['main']
weather = data['weather'][0]
temperature = main['temp']
humidity = main['humidity']
weather_description = weather['description']
print(f"Temperature: {temperature}°C")
print(f"Humidity: {humidity}%")
print(f"Weather Condition: {weather_description}")
else:
print("Error in the HTTP request")
if __name__ == "__main__":
api_key = "your_api_key_here"
city = input("Enter city name: ")
get_weather(api_key, city)