-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathertest.py
56 lines (42 loc) · 1.18 KB
/
weathertest.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
# Program to take a city name as input and return temperature in F
import requests
import json
url = "http://api.openweathermap.org/data/2.5/weather?appid=d0c61e2af77bea9ba7f337ad3f0495d6&q="
locurl="http://api.ipstack.com/check?access_key=01ba0bb834c723d9d47f061780376e45"
def temp_conv(val):
return 1.8*(val-273) + 32
def assign_cityname(ch):
if ch == "2":
res = requests.get(locurl)
locationobj = res.json()
return locationobj['city']
elif ch == "1":
#print (city)
#cityname = city
str = "Chicago"
return str
else :
#print(choice)
print("Incorrect input")
exit
def main():
print("Do you wish to enter a city or use current location? \n Press 1 for city and 2 for current\n")
#choice = input("")
choice = "1"
#cityname = ""
cityname = assign_cityname(choice)
if cityname!="":
final = url + cityname
response = requests.get(final)
jsonobject = response.json()
#print (jsonobject)
if jsonobject["cod"] != "404":
info = jsonobject["main"]
temp = info["temp"]
print(jsonobject["name"]+ " weather : \n" +
str(int(temp_conv(temp))) + " degrees Fahrenheit")
else:
print(" City Not Found ")
else:
print("Invalid input")
main()