I have these two functions which are nearly identical apart from the if statement in search_genre, is there any way to combine them into one?
The books variable is a dictionary where each value is a list of dictionaries, i.e.:
books = {'Genre1':[{name:value, author:value}, {dict}], 'Genre2':[{dict}], and so on}
view_library() prints all the books and search_genre() takes a user input to print all books of a specific genre
def view_library():
for genre in books:
print(f'Genre: {genre}')
for book in books[genre]:
for key, value in book.items():
print(f"{key.title()} : {value.title()}")
print()
def search_genre(genre_type):
for genre in books:
if genre_type == genre:
print(f'{genre_type}:')
for book in books[genre_type]:
for key, value in book.items():
print(f"{key.title()} : {value.title()}")
print()
Aucun commentaire:
Enregistrer un commentaire