본문으로 바로가기

class ShoppingCart(object):

    """Creates shopping cart objects

    for users of our fine website."""

    items_in_cart = {}

    def __init__(self, customer_name):

        self.customer_name = customer_name


    def add_item(self, product, price):

        """Add product to the cart."""

        if not product in self.items_in_cart:

            self.items_in_cart[product] = price

            print product + " added."

        else:

            print product + " is already in the cart."


    def remove_item(self, product):

        """Remove product from the cart."""

        if product in self.items_in_cart:

            del self.items_in_cart[product]

            print product + " removed."

        else:

            print product + " is not in the cart."



'잡동사니' 카테고리의 다른 글

Codecademy - Python. class basic code  (0) 2015.10.29
Codecademy - Python. battleship game  (0) 2015.10.18
[코드스터디] 5주차  (0) 2015.06.25
[코드스터디] 4주차  (0) 2015.06.25
[코드스터디] 3주차  (0) 2015.06.25