-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathweather.py
More file actions
29 lines (18 loc) · 787 Bytes
/
weather.py
File metadata and controls
29 lines (18 loc) · 787 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
28
29
import requests
from dotenv import load_dotenv
import os
from pprint import pprint
load_dotenv()
def get_current_weather():
print('\n*** Get Current Weather Conditions ***\n')
city = input("\nPlease enter a city name:\n")
request_url = f'https://api.openweathermap.org/data/2.5/weather?appid={os.getenv("API_KEY")}&q={city}&units=imperial'
# print(request_url)
weather_data = requests.get(request_url).json()
# pprint(weather_data)
print(f'\nCurrent weather for {weather_data["name"]}:')
print(f'\nThe temp is {weather_data["main"]["temp"]:.1f}°')
print(
f'\n{weather_data["weather"][0]["description"].capitalize()} and feels like {weather_data["main"]["feels_like"]:.1f}°\n')
if __name__ == "__main__":
get_current_weather()