Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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")