'''if the item is not lockedraise an exception if the item_type does not existraise an exception if the item is currently out of stockraise an exception if the item is availablesubtract one item and return the number of items left ''pass we could hand this object prototype to developer and have them implement the methods to do exactly as they say while we work on the code that needs to make purchase we'll use python' robust exception handling to consider different branchesdepending on how the purchase was madeitem_type 'widgetinv inventory(inv lock(item_typetrynum_left inv purchase(item_typeexcept invaliditemtypeprint("sorrywe don' sell {}format(item_type)except outofstockprint("sorrythat item is out of stock "elseprint("purchase complete there are "{{} leftformat(num_leftitem_type)finallyinv unlock(item_typepay attention to how all the possible exception handling clauses are used to ensure the correct actions happen at the correct time even though outofstock is not terribly exceptional circumstancewe are able to use an exception to handle it suitably this same code could be written with an if elif else structurebut it wouldn' be as easy to read or maintain we can also use exceptions to pass messages between different methods for exampleif we wanted to inform the customer as to what date the item is expected to be in stock againwe could ensure our outofstock object requires back_in_stock parameter when it is constructed thenwhen we handle the exceptionwe can check that value and provide additional information to the customer the information attached to the object can be easily passed between two different parts of the program the exception could even provide method that instructs the inventory object to reorder or backorder an item