Question

Please help with this exercise on Python program involving the use of dictionary. ### This exercise...

Please help with this exercise on Python program involving the use of dictionary.

### This exercise involves building a non-trivial dictionary.
### The subject is books.

### The key for each book is its title
### The value associated with that key is a dictionary

### ### In that dictionary there will be Three keys: They are all strings, they are:
### ### "Pages", "Author", "Publisher"

### ### ### "Pages" is associated with one value - an int

### ### ### "Author is associated with a dictionary as value
### ### ### ### That "Author" dictionary has two keys: "First", and "Last" each with a string value

### ### ### "Publisher" is associated with a dictionary as value
### ### ### ### That "Publisher" dict has one key "Location" with a string as value.

### An Example might look like:
### {"Harry Potter": {"Pages":200, "Author":{"First":"J.K", "Last":"Rowling"}, "Publisher":{"Location":"NYC"}},
### "Fear and Loathing in Las Vegas": { ...}}

### Code a function called "build_book_dict"
### ACCEPT five inputs, all lists of n-length
### ### A list of titles, pages, first<name>, last<name>, and <publisher>location.

### RETURN a dictionary as described above.
### Keys must be spelled just as they appear above - correctly and capitalized.

def build_book_dict(titles, pages, firsts, lasts, locations):
    """
    Return a nested dictionary storing information about Books
  
    Positional Arguments:
        titles -- A list of strings
        pages -- A list of ints
        firsts -- A list of strings
        lasts -- A list of strings
        locations -- A list of strings
  
    Example:
        titles = ["Harry Potter", "Fear and Loathing in Las Vegas"]
        pages = [200, 350]
        firsts = ["J.K.", "Hunter"]
        lasts = ["Rowling", "Thompson"]
        locations = ["NYC", "Aspen"]
      
        book_dict = build_book_dict(titles, pages, firsts, lasts, locations)
        print(book_dict) # -->
            {'Fear and Loathing in Las Vegas': {'Publisher': {'Location': 'Aspen'},
            'Author': {'Last': 'Thompson', 'First': 'Hunter'}, 'Pages': 350},
            'Harry Potter': {'Publisher': {'Location': 'NYC'},
            'Author': {'Last': 'Rowling', 'First': 'J.K.'}, 'Pages': 200}}
      
    """
  
    return dict()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

### This exercise involves building a non-trivial dictionary.
### The subject is books.

### The key for each book is its title
### The value associated with that key is a dictionary

### ### In that dictionary there will be Three keys: They are all strings, they are:
### ### "Pages", "Author", "Publisher"

### ### ### "Pages" is associated with one value - an int

### ### ### "Author is associated with a dictionary as value
### ### ### ### That "Author" dictionary has two keys: "First", and "Last" each with a string value

### ### ### "Publisher" is associated with a dictionary as value
### ### ### ### That "Publisher" dict has one key "Location" with a string as value.

### An Example might look like:
### {"Harry Potter": {"Pages":200, "Author":{"First":"J.K", "Last":"Rowling"}, "Publisher":{"Location":"NYC"}},
### "Fear and Loathing in Las Vegas": { ...}}

### Code a function called "build_book_dict"
### ACCEPT five inputs, all lists of n-length
### ### A list of titles, pages, first<name>, last<name>, and <publisher>location.

### RETURN a dictionary as described above.
### Keys must be spelled just as they appear above - correctly and capitalized.

def build_book_dict(titles, pages, firsts, lasts, locations):
        """
        Return a nested dictionary storing information about Books

        Positional Arguments:
        titles -- A list of strings
        pages -- A list of ints
        firsts -- A list of strings
        lasts -- A list of strings
        locations -- A list of strings

        Example:
        titles = ["Harry Potter", "Fear and Loathing in Las Vegas"]
        pages = [200, 350]
        firsts = ["J.K.", "Hunter"]
        lasts = ["Rowling", "Thompson"]
        locations = ["NYC", "Aspen"]

        book_dict = build_book_dict(titles, pages, firsts, lasts, locations)
        print(book_dict) # -->
                {'Fear and Loathing in Las Vegas': {'Publisher': {'Location': 'Aspen'},
                'Author': {'Last': 'Thompson', 'First': 'Hunter'}, 'Pages': 350},
                'Harry Potter': {'Publisher': {'Location': 'NYC'},
                'Author': {'Last': 'Rowling', 'First': 'J.K.'}, 'Pages': 200}}

        """

        result = {}

        for i in range(len(titles)):
                movieData = {}
                title = titles[i]
                movieData['Publisher'] = {'Location': locations[i]}
                movieData['Author'] = {'Last': lasts[i], 'First': firsts[i]}
                movieData['Pages'] = pages[i]
                result[title] = movieData

        return result

titles = ["Harry Potter", "Fear and Loathing in Las Vegas"]
pages = [200, 350]
firsts = ["J.K.", "Hunter"]
lasts = ["Rowling", "Thompson"]
locations = ["NYC", "Aspen"]

book_dict = build_book_dict(titles, pages, firsts, lasts, locations)
print(book_dict)
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Please help with this exercise on Python program involving the use of dictionary. ### This exercise...
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
  • MYSQL Questions: 1.For every author, display the number of books written by that author that is...

    MYSQL Questions: 1.For every author, display the number of books written by that author that is carried by Henry Books. Display the author number as ‘Author Number’, author name concatenated (first last) as ‘Author Name’, and the total number of books written by the author as ‘Number of Titles by Author’. List in descending order by author number. Limit the output to 10 rows. Insert your snip of the query and resultset together here. 2.Using a function, what are the...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

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