-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_test.py
executable file
·77 lines (65 loc) · 1.79 KB
/
weather_test.py
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
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
""" Use an open API example to return the weather """
# import required modules
import requests, json
# Enter your API key here
#api_key = "Your_API_Key"
api_key = "6c8908cb4b9e383b4af05947cde8d465"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = print("Enter City Name: ", input("Enter city name : "))
# create the complete_url variable
complete_url = f"{base_url}appid={api_key}&q={city_name}"
print(complete_url)
# 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 ")
###