Question

I do not understand why I receive an "invalid syntax" error. I did an hour of research with no success. I am running PYTHON 3. Where is the syntax error in this 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['respnose']['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'])
 request().close 
Was it helpful?

Solution

Several reasons:

  1. 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']))
    
  2. Your else statement is missing the : colon.

  3. 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']))
    
  4. Your indentation appears to be incorrect, but that could be a posting error.

  5. Your last line is not valid either; you want to call close(), not request():

    request.close()
    

    With urllib, you don't need to close the object, really.

  6. 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']))

OTHER TIPS

You need a : after else;

else:
      print "An error occurred: %s" % (json['error']['description'])

Number of ( and ) on this line are not equal:

>>> strs = """print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']"""
>>> strs.count('(')
3
>>> strs.count(')')
2

if-else should be properly indented like this:

if json['success']:
    ob = json['respnose']['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'])

The line

print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']

is missing a closing )

It should be

print ("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))

Python would probably be complaining about it in the next line

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top