玩蛇网提供最新Python编程技术信息以及Python资源下载!
python零基础培训
您现在的位置: 玩蛇网首页 > Python源码实例 > 正文内容

python class实现模拟购物车代码

<python class实现模拟购物车代码>
 
1. [代码][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)

站长推荐阅读相关内容:
linux基础入门教程

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/code/c878.html [复制]


微信扫描下图可直接关注Python公众号

玩蛇网Python QQ群,欢迎加入: ① 279974227 玩蛇网Python新手群
修订日期:2017年01月10日 - 09时33分42秒 发布自玩蛇网

我要分享到:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules