This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Bakebv
almost 10 years

Bug

File “python”, line 37 print “MP3 AUDIO: “ + story[‘audio’][0][‘format’][‘mp3’][0][‘$text’] + “\n” ^ SyntaxError: invalid syntax

This error keeps showing up for me, but the tasks are passing. I’ve copied the code over from the instructions and it still gives the same syntax error…

Answer 53af0d179c4e9d46da000443

0 votes

Permalink

I am also getting an error message. I’ve done everything and have quadruple checked my code to make sure it is correct but still to no avail. Could someone please tell me what is wrong?

print 'MP3 AUDIO: ' + story['audio'][0]['format']['mp3'][0]['$text'] + "\n"

Oops, try again. Check the line where you print ‘MP3 AUDIO:’. Don’t forget a ‘\n’ at the end of each line.

points
Submitted by Nhi Nguyen
almost 10 years

Answer 53c7094152f86382470003eb

0 votes

Permalink

i just figured out how to fix this, it’s all about the indentation:

from urllib2 import urlopen
from json import load, dumps

url = 'http://api.npr.org/query?apiKey=' 
key = 'API_KEY'
url = url + key
url += '&numResults=1&format=json&id=1007' #1007 is science
url += '&requiredAssets=audio,text,image'

response = urlopen(url)
json_obj = load(response)

# uncomment 3 lines below to see JSON output to file
f = open('output.json', 'w')
f.write(dumps(json_obj, indent=4))
f.close()

for story in json_obj['list']['story']:
    print "TITLE: " + story['title']['$text'] + "\n"
    print "DATE: " + story['storyDate']['$text'] + "\n"
    print "TEASER: " + story['teaser']['$text'] + "\n"
    if 'byline' in story:
        print "BYLINE: " + story['byline'][0]['name']['$text'] + "\n"
        if 'show' in story:
            print "PROGRAM: " + story['show'][0]['program']['$text'] + "\n"
            print "NPR URL: " + story['link'][0]['$text'] + "\n"
            print "IMAGE: " + story['image'][0]['src'] + "\n"
        if 'caption' in story:
            print "IMAGE CAPTION: " + story['image'][0]['caption']['$text'] + "\n"
        if 'producer' in story:
            print "IMAGE CREDIT: " + story['image'][0]['producer']['$text']  +  "\n"
            print "MP3 AUDIO: " + story['audio'][0]['format']['mp3']['$text']  + "\n"

all the “if” statements starting with “if show in story” need to be along the same indentation. once i did that my code passed just fine.

points
Submitted by arduinna
over 9 years