You will be able to extend this example also for books
If you follow our previous hint, you now have a bookstore that looks something like:
bookstore = {
'name': "Rmotr's bookstore"
'authors': [poe, borges, austen],
'books': [raven, ficciones]
}
We need now to add different authors. And the question is, "what is an author?". We need to represent, in a single data structure, an author with her name, nationality and an autogenerated ID. A dictionary might also be handy in this situaton:
author = {
'name': 'Edgar Allan Poe',
'nationality': 'US',
'id': ...
}
An author is created (and added to the bookstore at the same time) with the add_author
function. This function receives the bookstore, the name of the author and the nationality of the author. We'll then add this "author" to the list of authors
in the bookstore and also make sure we add the correct ID for that author.
def add_author(bookstore, name, nationality):
bookstore['last_author_id'] += 1
author = {
'name': name,
'nationality': nationality,
'id': bookstore['last_author_id']
}
bookstore['authors'].append(author)
return author
Now, we need to, for example, search by name. We know that authors then is just a list of dictionaries containing the name, nationality and ID of the author. We could do something like:
name_to_search = 'James Joyce'
for author in authors:
if author['name'] == name_to_search:
return author