· 5 years ago · Dec 18, 2020, 09:10 PM
1import requests
2
3## Intro- printed
4hello = input("Welcome to my Weather Program! -- Press Enter to Continue")
5
6## If user requests info by city
7def by_city():
8 city = input('Please Enter Your City Name: ')
9 ## where to request from- my API key
10 url = hidden
11 res = requests.get(url)
12 ## transferring data to Json for readability
13 data = res.json()
14
15 try:
16
17 ## Request try/except block
18 if(res.status_code == 200): ## It worked!
19 print("We did it! Connecting now.")
20 show_data(data)
21
22 else: ## Exceptions
23 if res.status_code == 404 or res.status_code == 400:
24 ## Didn't work. Location not found or bad request
25 print("Oh no! That didn't work, I'm sorry.")
26
27 except requests.exceptions.RequestException as reqEx:
28 exit('Unable to connect to the weather service, try again later.')
29
30
31 ## does the user want to go again?
32 question = input('Do you want to do another search ? Type Yes or No: ')
33 if question == 'Yes' or question == 'yes':
34 main()
35 if question == 'No' or question == 'no':
36 print("Thank you for using my weather program. Have a nice day!")
37 exit()
38
39## If user requests info by zip code
40def by_zip():
41 zip_code = int(input('Please Enter Your Zip Code: '))
42 ## where to request from- my API key
43 url = hidden
44 res = requests.get(url)
45 ## transferring data to Json for readability
46 data = res.json()
47
48 try:
49
50 ## Request try/except block
51 if(res.status_code == 200): ## It worked!
52 print("We did it! Connecting now.")
53 show_data(data)
54
55 else: ## Exceptions
56 if res.status_code == 404 or res.status_code == 400:
57 ## Didn't work. Location not found or bad request
58 print("Oh no! That didn't work, I'm sorry.")
59
60 except requests.exceptions.RequestException as reqEx:
61 exit('Unable to connect to the weather service, try again later.')
62
63 ## does the user want to go again?
64 question = input('Do you want to do another search ? Type Yes or No: ')
65 if question == 'Yes' or question == 'yes':
66 main()
67 if question == 'No' or question == 'no':
68 print("Thank you for using my weather program. Have a nice day!")
69 exit()
70
71## This will show data based on the user's request.
72def show_data(data):
73 temp = data['main']['temp']
74 hightemp = data['main']['temp_max']
75 lowtemp = data['main']['temp_min']
76 wind_speed = data['wind']['speed']
77 press = data['main']['pressure']
78 latitude = data['coord']['lat']
79 longitude = data['coord']['lon']
80 humid = data['main']['humidity']
81 description = data['weather'][0]['description']
82
83 print('Current Temperature : {} degrees fahrenheit'.format(temp))
84 print('High Temperature : {} degrees fahrenheit'.format(hightemp))
85 print('Low Temperature : {} degrees fahrenheit'.format(lowtemp))
86 print('Wind Speed : {} m/s'.format(wind_speed))
87 print('Pressure : {} hPa'.format(press))
88 print('Latitude : {}'.format(latitude))
89 print('Longitude : {}'.format(longitude))
90 print('Humidity : {} %'.format(humid))
91 print('Description : {}'.format(description))
92
93## Main function!
94def main():
95 while True:
96 answer = input("If you would like to request weather information, please choose from the following.\nType C to request information by city.\nType Z to request information by zip code.\n")
97 if answer == 'C' or answer == 'c':
98 by_city()
99
100 if answer == 'z' or answer == 'Z':
101 by_zip()
102
103 else:
104 print("Unfortunately, that is not an option we support. Please try again.")
105
106main()