Created
December 7, 2022 15:43
-
-
Save mhassanist/dc26d9c96ac818bab9cfb06053e9f5bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# import required modules | |
import requests, json | |
# Enter your API key here | |
api_key = "10562986aab4aacd5e1a4e38a655f13b" | |
# base_url variable to store url | |
base_url = "http://api.openweathermap.org/data/2.5/weather?" | |
# Give city name | |
city_name = input("Enter city name : ") | |
# complete_url variable to store | |
# complete url address | |
complete_url = base_url + "appid=" + api_key + "&q=" + city_name | |
# get method of requests module | |
# return response object | |
response = requests.get(complete_url) | |
# json method of response object | |
# convert json format data into | |
# python format data | |
x = response.json() | |
# Now x contains list of nested dictionaries | |
# Check the value of "cod" key is equal to | |
# "404", means city is found otherwise, | |
# city is not found | |
if x["cod"] != "404": | |
# store the value of "main" | |
# key in variable y) | |
y = x["main"] | |
# store the value corresponding | |
# to the "temp" key of y | |
current_temperature = y["temp"] | |
# store the value corresponding | |
# to the "pressure" key of y | |
current_pressure = y["pressure"] | |
# store the value corresponding | |
# to the "humidity" key of y | |
current_humidity = y["humidity"] | |
# store the value of "weather" | |
# key in variable z | |
z = x["weather"] | |
# store the value corresponding | |
# to the "description" key at | |
# the 0th index of z | |
weather_description = z[0]["description"] | |
# print following values | |
print(" Temperature (in kelvin unit) = " + str(current_temperature) + | |
"\n atmospheric pressure (in hPa unit) = " + str(current_pressure) + | |
"\n humidity (in percentage) = " + str(current_humidity) + | |
"\n description = " + str(weather_description)) | |
else: | |
print(" City Not Found ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment