python 学习,使用类模拟购物车代码
当前位置:以往代写 > Python教程 >python 学习,使用类模拟购物车代码
2019-06-14

python 学习,使用类模拟购物车代码

python 学习,使用类模拟购物车代码
python">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."

my_cart=ShoppingCart("wangweiwei")
my_cart.add_item("apple",12)

标签:python

    关键字:

在线提交作业