Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
16 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import requests #This library is necessary to get access to URL and its data if a web allows. | ||
|
||
'''The function is giving the temperature and short weather condition for a particular location. ''' | ||
def weather_ask(sentence): | ||
try: | ||
city = sentence | ||
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&APPID=f7d590dc5f61801f09e17f3922567a55' #The variable city can be either from stored data (global variable) or from user input (when asking for a different location). | ||
res = requests.get(url) | ||
data = res.json() #JSON format is necessary to get the data from the website. | ||
temp = data['main']['temp'] #Choose which data wants to get from the web page, in this case, it is temperature. | ||
temp_C = temp - 273.15 #Making conversion from Kelvin degrees into Celsius degrees. | ||
weather_description = data['weather'][0]['description'] #Weather description data is taken from web page. | ||
print('Temperature is: ', round(temp_C, 1), 'degree and', weather_description) | ||
except: | ||
print("Invalid command this is used by typing the following: weather in [city] OR weather for [city] where [city] is replaced with the city you want to find the weather for") | ||
|