Skip to content

Commit 247e4d8

Browse files
Another chapter ends
1 parent 7727916 commit 247e4d8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/python3.5
2+
3+
4+
"""
5+
6+
Author: Samrat Banerjee
7+
Dated: 08/09/2018
8+
Description: Project: Prints the weather for a location from the command line.
9+
10+
"""
11+
12+
import json, requests, sys
13+
14+
# Compute location from command line arguments.
15+
if len(sys.argv) < 2:
16+
print('Usage: quickWeather.py location')
17+
sys.exit()
18+
location = ' '.join(sys.argv[1:])
19+
20+
# Download the JSON data from OpenWeatherMap.org's API.
21+
url ='http://api.openweathermap.org/data/2.5/forecast/daily?q=%s&cnt=3' % (location)
22+
response = requests.get(url)
23+
response.raise_for_status()
24+
25+
# Load JSON data into a Python variable.
26+
weatherData = json.loads(response.text)
27+
28+
# Print weather descriptions.
29+
w = weatherData['list']
30+
print('Current weather in %s:' % (location))
31+
print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])
32+
print()
33+
print('Tomorrow:')
34+
print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])
35+
print()
36+
print('Day after tomorrow:')
37+
print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])

0 commit comments

Comments
 (0)