Skip to content

Instantly share code, notes, and snippets.

@NurseLaura
Created October 7, 2016 01:06
Show Gist options
  • Save NurseLaura/45651556bb046d0ea66570c44c672a95 to your computer and use it in GitHub Desktop.
Save NurseLaura/45651556bb046d0ea66570c44c672a95 to your computer and use it in GitHub Desktop.
milk = {'weight' : 7.8,'name': 'Milk', 'price': 2.99, 'taxable': False, 'upc': '0001'}
apples = {'weight' : 3.1, 'name': 'Apples', 'price': 3.99, 'taxable': False, 'upc': '0002'}
bread = {'weight' : 1.25, 'name': 'Bread', 'price': .99, 'taxable': False, 'upc': '0003'}
beer = {'weight' : 4.9, 'name': 'Beer', 'price': 7.99, 'taxable': True, 'upc': '0004'}
chips = {'weight' : 0.96, 'name': 'Chips', 'price': 1.50, 'taxable': False, 'upc': '0005'}
wine = {'weight' : 3.18, 'name': 'Red Wine', 'price': 9.99, 'taxable': True, 'upc': '0006'}
oreos = {'weight' : 1.08, 'name': 'Double Stuf Oreos', 'price': 2.50, 'taxable': False, 'upc': '0007'}
sushi = {'weight' : 1.65, 'name': 'Veggie Sushi', 'price': 5.99, 'taxable': False, 'upc': '0008'}
sprite = {'weight' : 2.82, 'name': 'Sprite', 'price': 3.99, 'taxable': True, 'upc': '0009'}
candy = {'weight' : 0.43, 'name': 'Sour Patch Kids', 'price': 1.99, 'taxable': True, 'upc': '0010'}
class Cart(object):
def __init__(self, items = []):
self.items = items
def add_item(self, item):
self.items.append(item)
def item_count(self):
return len(self.items)
def subtotal(self):
subtotal = 0
for item in self.items:
subtotal = subtotal + item['price']
return subtotal
def tax(self):
tax = 0
for item in self.items:
if item['taxable']:
tax = tax + item['price'] * .07
return round(tax, 2)
def grand_total(self):
return self.subtotal() + self.tax()
def print_receipt(self):
print('Thanks for shopping at our store!')
print('Total items: %s' % self.item_count())
for item in self.items:
print('%s: $%s' % (item['name'], item['price']))
print('Subtotal: $%s' % self.subtotal())
print('Tax: $%s' % self.tax())
print('Total: $%s' % self.grand_total())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment