I am tasked with the following question, but cannot come up with the right code:
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 Lothing in Las Vegas": { ...}}
Code a function called "build_book_dict"
ACCEPT five inputs, all lists of n-length
A list of titles, pages, first, last, and location.
RETURN a dictionary as described above. Keys must be spelled just as they appear above - correctly and capitalized.
Here is an example:
titles = ["Harry Potter", "Fear and Lothing 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 Lothing 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}}
My code currently looks the following:
def build_book_dict(titles, pages, firsts, lasts, locations):inputs = zip(titles, pages, firsts, lasts, locations)for titles, pages, firsts, lasts, locations in inputs:dict = {titles : {"Pages" : pages,"Author" : {"First" : first,"Last" : last},"Publisher" : {"Location" : locations},},}return dict
But it only stores the information of the last "book".