Lab 20 - Shopping Cart

Typically, an online store will have the concept of a “shopping cart”. In this problem we implement such a shopping cart.

Start from the file called shopping.py. In this file create a class called ShoppingCart. The class will have the following structure:

  • An attribute contents, which is a list containing the names of what the user wants to buy.
  • Methods:
    • buy, which takes one parameter – the name of the product the user wants to buy. It returns nothing.
    • total_cost, which takes no parameters and returns the total cost of everything in the list contents.

The starter code contains a price dictionary you will use to calculate the total cost.

Here are some examples illustrating the expected behavior:

>>> %Run interval.py
>>> cart = ShoppingCart()
>>> cart.buy("Boots")
>>> cart.buy("FitBit")
>>> cart.total_cost()
220

Back to Top