# Imagine you are writing a software that maintains # an inventory for the "Awesome" book store. # The software contains five main menu options: # # 1. add_book(inventory, book_info) # - add the book's title and information # to the inventory dictionary if the title # has not been added. # (you decide what to do if the book has already been added; # for example, display an informative message) # # 2. update_book(inventory, book_info) # - update the book's information in the inventory dictionary # if the book title has been added. # (you decide what to do if the book does not exist in the inventory; # for example, display an informative message) # # 3. add_quantity(inventory, book_title, quantity) # - add the quantity to the specified book's title # (you should consider -- how to handle if the book does not exist) # # 4. reduce_quantity(inventory, book_title, quantity) # - reduce the quantity of the specified book's title # (you should consider -- how to handle if the book does not exist) # # 5. print_inventory(inventory) # - display the inventory in an easy to read format # (you decide what format is easy to read, don't simply print a dict) # inventory should now contain # {'Intro to Python': {'year': 2019, 'price': 60, 'quantity': 75}} # book1 = {'title': 'Intro to Python', 'year': 2019, 'price': 60, 'quantity': 75} def add_book(inventory, book_info): if book_info['title'] in inventory.keys(): # look in keys print('cannot add') else: title = book_info.pop('title') inventory[title] = book_info # def update_book(inventory, book_info): # def add_quantity(inventory, book_title, quantity): # def reduce_quantity(inventory, book_title, quantity): def print_inventory(inventory): print("==== Awesome store book inventory ==== \n") book_inventory = {} book1 = {'title': 'Intro to Python', 'year': 2019, 'price': 60, 'quantity': 75} book2 = {'title': 'Python - banana', 'year': 2018, 'price': 64, 'quantity': 100} book3 = {'title': 'Python Master', 'year': 2018, 'price': 70, 'quantity': 59} add_book(book_inventory, book1) # inventory should now contain # {'Intro to Python': {'year': 2019, 'price': 60, 'quantity': 75}} add_book(book_inventory, book2) add_book(book_inventory, book3) print(book_inventory) # print_inventory(book_inventory) # make sure that we cannot re-add the same book # add_book(book_inventory, book1) # notice: KeyError, why? # This is because book1 was passed by reference. # When the add_book function pops the title from book_info, # it reflects book1 and therefore the title no longer exists in book1 # To test whether re-add behaves properly, # reassign book1 book1 = {'title': 'Intro to Python', 'year': 2020, 'price': 60, 'quantity': 75} add_book(book_inventory, book1) # print('---- inventory after attempting to re-add the same book title ----') # print_inventory(book_inventory) # update_book(book_inventory, book1) # print('---- inventory after updating a book ----') # print_inventory(book_inventory) # # book4 = {'title': 'New Python', 'year': 2022, 'price': 45, 'quantity': 50} # update_book(book_inventory, book4) # print('---- inventory after after attempting to update a non-existent book ----') # print_inventory(book_inventory) # # # add_quantity(book_inventory, 'Intro to Python', 20) # print('---- inventory after adding quantity ----') # print_inventory(book_inventory) # # # add_quantity(book_inventory, 'New Python', 20) # print('---- inventory after attempting to add quantity to a non-existent book ----') # print_inventory(book_inventory) # # # reduce_quantity(book_inventory, 'Intro to Python', 10) # print('---- inventory after reducing quantity ----') # print_inventory(book_inventory) # # # reduce_quantity(book_inventory, 'New Python', 20) # print('---- inventory after attempting to reduce quantity to a non-existent book ----') # print_inventory(book_inventory)