Several reasons:
Your parenthesis are not balanced:
print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']
That's one closing parenthesis missing, and the one you have is in the wrong position.
This should be:
print ("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
Your
else
statement is missing the:
colon.Your second
print
function is not a function, it pretends to be a Python 2 statement instead. Correct it by adding parenthesis:print("An error occurred: %s" % (json['error']['description']))
Your indentation appears to be incorrect, but that could be a posting error.
Your last line is not valid either; you want to call
close()
, notrequest()
:request.close()
With
urllib
, you don't need to close the object, really.You misspelled
respnose
:ob = json['response']['ob']
Working code:
from urllib.request import urlopen
import json
request = urlopen("http://api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO")
response = request.read().decode("utf-8")
json = json.loads(response)
if json['success']:
ob = json['response']['ob']
print("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
else:
print("An error occurred: %s" % (json['error']['description']))