-4

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 
George Cummins
  • 28,485
  • 8
  • 71
  • 90
apples723
  • 105
  • 2
  • 2
  • 8
  • Have you at least read [wikipedia python syntax summary page](http://en.wikipedia.org/wiki/Python_syntax_and_semantics)? – J0HN Jun 27 '13 at 17:02
  • i use wiki books but not wikipedia thats what i was taught in school dont use wilikpediea – apples723 Jun 27 '13 at 17:20
  • 1
    and I use official documentation and mailing lists, that what I've learnt from professional career. :) But even wikipedia knowledge would suffice to fix this :) – J0HN Jun 27 '13 at 17:22
  • 1
    This question does not even include the error message encountered. – Adrian Panasiuk Jun 27 '13 at 19:29
  • it does to it has invaild syntax thats the only error there is no track back error – apples723 Jun 28 '13 at 15:23

3 Answers3

8

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']))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

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'])
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

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

RedBaron
  • 4,717
  • 5
  • 41
  • 65
  • ok i changed it and get this raceback (most recent call last): File "C:/Python33/sddf.py", line 8, in ob = json['respnose']['ob'] KeyError: 'respnose' – apples723 Jun 27 '13 at 17:05