Skip to content
Permalink
Browse files
Added comments
  • Loading branch information
gorausa committed Nov 29, 2018
1 parent e7de25e commit 6ae349c5e9a9791dedba169494fae4470936cf8e
Showing 1 changed file with 16 additions and 0 deletions.
@@ -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")

0 comments on commit 6ae349c

Please sign in to comment.