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." def add_item(self, food_item, price): if price < 10: self.items_in_cart[food_item] = price print self.items_in_cart my_cart = ShoppingCart("apple") print my_cart.add_item("apple", 6)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question