· 5 years ago · Dec 18, 2020, 06:26 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 = 'https://api.openweathermap.org/data/2.5/weather?q={},us&appid=23caf59a438de59d16d366601b1a9918&units=imperial'.format(city)
11 res = requests.get(url)
12 ## transferring data to Json for readability
13 data = res.json()
14 show_data(data)
15
16
17 ## does the user want to go again?
18 question = input('Do you want to do another search ? Type Yes or No: ')
19 if question == 'Yes' or question == 'yes':
20 main()
21 if question == 'No' or question == 'no':
22 print("Thank you for using my weather program. Have a nice day!")
23 exit()
24
25## If user requests info by zip code
26def by_zip():
27 zip_code = int(input('Please Enter Your Zip Code: '))
28 ## where to request from- my API key
29 url = 'https://api.openweathermap.org/data/2.5/weather?zip={},us&units=imperial&appid=23caf59a438de59d16d366601b1a9918'.format(zip_code)
30 res = requests.get(url)
31 ## transferring data to Json for readability
32 data = res.json()
33 show_data(data)
34
35 ## does the user want to go again?
36 question = input('Do you want to do another search ? Type Yes or No: ')
37 if question == 'Yes' or question == 'yes':
38 main()
39 if question == 'No' or question == 'no':
40 print("Thank you for using my weather program. Have a nice day!")
41 exit()
42
43## This will show data based on the user's request.
44def show_data(data):
45 temp = data['main']['temp']
46 hightemp = data['main']['temp_max']
47 lowtemp = data['main']['temp_min']
48 wind_speed = data['wind']['speed']
49 press = data['main']['pressure']
50 latitude = data['coord']['lat']
51 longitude = data['coord']['lon']
52 humid = data['main']['humidity']
53 description = data['weather'][0]['description']
54
55 print('Current Temperature : {} degrees fahrenheit'.format(temp))
56 print('High Temperature : {} degrees fahrenheit'.format(hightemp))
57 print('Low Temperature : {} degrees fahrenheit'.format(lowtemp))
58 print('Wind Speed : {} m/s'.format(wind_speed))
59 print('Pressure : {} hPa'.format(press))
60 print('Latitude : {}'.format(latitude))
61 print('Longitude : {}'.format(longitude))
62 print('Humidity : {} %'.format(humid))
63 print('Description : {}'.format(description))
64
65## Main function!
66def main():
67 while True:
68 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")
69 if answer == 'C' or answer == 'c':
70 try:
71 by_city()
72
73 except Exception:
74 print("That didn't work. Try again, please!")
75 by_city()
76 if answer == 'z' or answer == 'Z':
77 try:
78 by_zip()
79
80 except Exception:
81 print("That didn't work. Try again, please!")
82 by_zip()
83 else:
84 print("Unfortunately, that is not an option we support. Please try again.")
85
86main()