This forum is now read-only. Please use our new forums! Go to forums
13/14 says i didnt put "\n"?
okay, so to my knowledge my code is correct, i went back and corrected any errors it had given me prior, which actually was the same error that has stumped me now. It is currently saying check the line where you print MP3 AUDIO, and make sure you put a space at the end of each line, well as you’ll see in my code, the line it references does indeed have a space at the end. it did this before with the IMAGE line, but that was due to no semi colons at the end of if statements and some bad indentation, which i fixed promptly, and has now brought me to this error. I’ve searched my code and cant find the error, maybe someone can and direct me towards fixing the issue. I believe its something in the bottom lines of code, because nothing after IMAGE gets printed to the output box, so no image caption, etc. Here’s my code.
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'][0]['$text'] + "\n"
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency