Question

Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and...

Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a year and a category, and returns a sorted list of the surnames for the winner(s) in that category for that year (up to three people can share the prize). The year will be a string (e.g. "1975"), not a number. The categories are: "chemistry", "economics", "literature", "peace", "physics", and "medicine". The JSON file will be named nobels.json and will be provided - you do not need to submit it.

For example, your class could be used like this:

The file must be named: NobelData.py

I'm stuck on this problem. This is what I have so far but I am getting errors.

import json

class NobelData:

    def __init__(self):
        response = json.get("http://api.nobelprize.org/v1/prize.json")
        self.data = response.json()

    def search_nobel(self, year, category):
        a = len(self.data['prizes'])
        winners = []
        for i in range(0, a):
            if (self.data['prizes'][i]['year']==year and self.data['prizes'][i]['category']==category):
                winners = self.data['prizes'][i]['laureates']
                break
        b = len(winners)
        w_sur = []
        for i in range(0, b):
            w_sur.append(winners[i]['surnames'])
        w_sur.sort()
        return w_sur


nd = NobelData()
w_sur = nd.search_nobel("2001", "economics")
print(w_sur)

surnames = ["chemistry", 'economics', 'literature', 'peace', 'physics', 'medicine']
nd = NobelData()
nd.search_nobel("2001", "economics")
"http://api.nobelprize.org/v1/prize.json"
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import json
import requests

class NobelData:

def __init__(self):
response = requests.get("http://api.nobelprize.org/v1/prize.json")
self.data = response.json()

def search_nobel(self, year, category):
a = len(self.data['prizes'])
winners = []
for i in range(0, a):
if (self.data['prizes'][i]['year']==year and self.data['prizes'][i]['category']==category):
winners = self.data['prizes'][i]['laureates']
break
b = len(winners)
w_sur = []
for i in range(0, b):
w_sur.append(winners[i]['surname'])
w_sur.sort()
return w_sur


nd = NobelData()
w_sur = nd.search_nobel("2001", "economics")
print(w_sur)

surnames = ["chemistry", 'economics', 'literature', 'peace', 'physics', 'medicine']
nd = NobelData()
nd.search_nobel("2001", "economics")

I have fixed the errors in the code. still you have any problem with execution kindly comment.

If you have any doubts please comment and please don't dislike.

Add a comment
Know the answer?
Add Answer to:
Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python 3 needs to read a local JSON file - it doesn't need to access the...

    python 3 needs to read a local JSON file - it doesn't need to access the internet. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as...

  • Write a class named SatData that reads a JSON file containing data on 2010 SAT results...

    Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes the data to a text file in CSV (comma-separated values) format. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named save_as_csv that takes as a parameter a list of DBNs (district...

  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT