Created
December 7, 2014 15:35
-
-
Save drvinceknight/3580076a2639339f4422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cost_of_items = {'apple': 4, | |
'cake': 6, | |
'doughnut': 12} | |
class ShoppingBasket: | |
""" | |
A class for a shopping basket with attributes for the cost and the contents of a shopping basket | |
""" | |
def __init__(self): | |
self.cost = 0 # Initialising a cost of 0 (basically the value of the basket when someone picks it up | |
self.contents = [] # Initialising the contents of the basket as an empty list | |
def add_item(self, item): | |
""" | |
A method to add an item to the basket, this appends it to the list but also updates the cost | |
""" | |
self.contents.append(item) | |
self.cost += cost_of_items[item] | |
basket = ShoppingBasket() | |
print basket.cost | |
print basket.contents | |
print 20 * '=' # We're putting cake in the basket | |
basket.add_item('cake') | |
print basket.cost | |
print basket.contents | |
print 20 * '=' # We're putting cake in the basket again (cake is nice) | |
basket.add_item('cake') | |
print basket.cost | |
print basket.contents | |
print 20 * '=' # We're putting an apple in the basket | |
basket.add_item('apple') | |
print basket.cost | |
print basket.contents | |
print 20 * '=' # We're putting a doughnut in the basket | |
basket.add_item('doughnut') | |
print basket.cost | |
print basket.contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment