Manage the Shopping Cart
Given an empty shopping cart, you are asked to implement a manage_cart
function that would be called n
times to modify and view the shopping cart. There are 3 allowed operations:
add
: Add an item to the cart (an item is a string). If the item is already in the cart, the program should print<ITEM> is already in the cart
.remove
: Remove an item from the cart (an item is a string). If the item is not in the cart, the program should print<ITEM> was not found
.view
: PrintCurrent cart: <ITEM1>, <ITEM2>, ...
. If the cart is empty, it should just printCurrent cart:
.
Here <ITEM>
, <ITEM1>
, and <ITEM2>
are the items (the strings) currently in the shopping cart or passed as an argument.
The manage_cart
function should have 2 parameters:
operation
(positional): One ofadd
,remove
, orview
.item
։ An optional keyword argument that defaults toNone
.
Tip
Store a global cart
variable that would store the current contents of the cart.
Input | Output |
---|---|
2 add Banana view | Current cart: Banana |
7 add Apple add Banana view remove Apple add Banana view remove Apple | Current cart: Apple, Banana Banana is already in the cart Current cart: Banana Apple was not found |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB