This forum is now read-only. Please use our new forums! Go to forums
Error: Image CAPTION and CREDIT not displayed.
This is the entire code. Bold ones are supposed to be part of 2.5 excercise.
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=image,text,audio’
response = urlopen(url) json_obj = load(response)
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 "PRGORAM: " + 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"**
But, the error displayed is Oops, try again. Did you print each element exactly, followed by “\n”?
After examining the result, i found that IMAGE CAPTION and CREDIT are missing. …. IMAGE: http://media.npr.org/assets/img/2013/01/10/butter-digest-1_toned_wide-64b41e191857910ffaad85488603bcaa1b5442bb.jpg?s=12 MP3 AUDIO: http://api.npr.org/m3u/1169117499-313ea6.m3u?orgId=1&topicId=1053&ft=3&f=1007 …
Answer 50f6c61cb326bbfc19004545
Hi Lord Sauron,
Sorry that took so long, I unplugged over the weekend. I figured it out. There’s a little typo in your script. You print "PRGORAM: "
when it should be "PROGRAM: "
. I’m sorry my error messages are so general instead of showing where the problem is. I’ll see if I can correct that.
When I copied your code from above and ran it, I had to reset all the spacing and remove the “**” bold tags. You might have to do the same.
Let me know if that works!
Answer 50f9f16787690fcabe00a4e1
You’re right that caption and image credit aren’t shown.
for them to show you need to check if those keys exist in story[‘image’][0], not in story. However if you check in the right place the assignment doesn’t validate!
Answer 51824580385d659ae20032bb
please use the ‘code indent button’ to format code as code:
Answer 50fec3d37d0fc4ca150001db
Hi RubySurfer. I posted this in another forum but checking ror story['image'][0]
can cause a KeyError:
Also, you are validating for byline, show, and image caption/credit by checking to see if the key returns True, like this:
if story['byline']:
Unfortunately, if the story doesn’t have a byline, it won’t have that story['byline']
key and it will return a Python KeyError
.
That’s why I used in
to check keys like this:
if 'byline' in story:
If I was writing a real app, I would probably use try: ... except KeyError:
to check for the key, but since we were doing so many of these, I wanted to keep the code short.
You do not need to check for 'image'
to pass the test (we required it in the API) but you do need to check for 'caption'
and 'producer'
since not all images have those.
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
1 comments
Hello Javaun- I don’t understand why checking for caption and producer in the story is the right thing to do. Wouldnt that return true if ANY of the images have captions or producers? Don’t we just want to check the first object in the image list for those resources?