-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
65 lines (60 loc) · 2.22 KB
/
weather.py
File metadata and controls
65 lines (60 loc) · 2.22 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
from requests.exceptions import HTTPError
import json
def weather():
greeting = "Welcome to Facade ventures weather app"
print(greeting)
name = str(input("Please enter your full name (Firstname Lastname): "))
print(name + " welcome to Fecade Ventures")
print("Please select one of the city number [1-5] below")
a = 'Johannesburg'
b = 'Lusaka'
c = 'Abuja'
d = 'Nairobi'
e = 'Accra'
print(1, a)
print(2, b)
print(3, c)
print(4, d)
print(5, e)
city = int(input("Please select the city number here: "))
if city == 1:
api_city = a
country = "South Africa"
print("You selected ", a)
elif city == 2:
api_city = b
country = "Zambia"
print("You selected ", b)
elif city == 3:
api_city = c
country = "Nigeria"
print("You selected ", c)
elif city == 4:
api_city = d
country = "Kenya"
print("You selected ", d)
elif city == 5:
api_city = e
country = "Ghana"
print("You selected ", e)
api_key ='a670aae98eb56d4e2aca533972074e76'
url = f'https://api.openweathermap.org/data/2.5/weather?q={api_city}&appid={api_key}'
# url ="https://community-open-weather-map.p.rapidapi.com/weather"
try:
response = requests.get(url)
except HTTPError as err:
print(f'Http Error occurred: {err}')
else:
print(f" {name} Please wait while we retrieve your information \n Retrieving infor for {api_city} State in {country}...")
json_response = response.json()
# print(json_response)
description = json_response['weather'][0]['description']
cloud = json_response['clouds']['all']
wind = json_response['wind']['speed']
min_temp = json_response['main']['temp_min']
mc_temp =(min_temp - 273.15)
max_temp = json_response['main']['temp_max']
xc_temp =(max_temp - 273.15)
print(f'Todays forecast for {api_city} State is {description} \n It is also {cloud}% cloudy in {api_city} state today \n with minimum temperature of {int(mc_temp)}°C and \n maximum temperature of {int(xc_temp)}°C \n the wind blows at {wind}m/s ' )
weather()