[Tutor] Tutor Digest, Vol 157, Issue 24
Eloka Chima
chima_eloka at yahoo.com
Fri Mar 10 05:50:17 EST 2017
Thank you so much. Your guidance have been helpful, the bugs have been gotten rid of after I reviewed the code. But 3 out of 6 of the test is correct, the other 3 is incorrect. Take a look at it and tell me where I went wrong.
>> Below is my new code:
class ShoppingCart(object): def __init__(self): self.total = 0 self.items = {} def add_item(self, item_name, quantity, price): for item_name in self.items: if not item_name in self.items: self.items["item_name"] = quantity self.total += price return self.total return self.items def remove_item(self, item_name, quantity, price): for item_name in self.items: if item_name in self.items: del self.items["item_name"] self.total -= price else: if quantity > self.items[quantity]: self.items = 0 return self.items def checkout(self, cash_paid): if cash_paid < self.total : self.total -= cash_paid return "Cash paid not enough"class Shop(ShoppingCart): def __init__(self): self.quantity = 100 def remove_item(self): self.quantity -=1 return self.quantity
>> This is the test for the program:
import unittest
class ShoppingCartTestCases(unittest.TestCase): def setUp(self): self.cart = ShoppingCart() self.shop = Shop() def test_cart_property_initialization(self): self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct') self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary') def test_add_item(self): self.cart.add_item('Mango', 3, 10) self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items') self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item') def test_remove_item(self): self.cart.add_item('Mango', 3, 10) self.cart.remove_item('Mango', 2, 10) self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item') self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item') def test_checkout_returns_correct_balance(self): self.cart.add_item('Mango', 3, 10) self.cart.add_item('Orange', 16, 10) self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct') self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct') def test_shop_is_instance_of_shopping_cart(self): self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
def test_shop_remove_item_method(self): for i in range(15): self.shop.remove_item()
self.assertEqual(self.shop.quantity, 85)
>> I get this test result after running it:
1 . test_add_itemFailure in line 20, in test_add_item self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items') AssertionError: Cart total not correct after adding items2 . test_checkout_returns_correct_balanceFailure in line 34, in test_checkout_returns_correct_balance self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct') AssertionError: Balance of checkout not correct3 . test_remove_itemFailure in line 27, in test_remove_item self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item') AssertionError: Cart total not correct after removing item.
On Thursday, 9 March 2017, 21:39, "tutor-request at python.org" <tutor-request at python.org> wrote:
----- Forwarded Message -----
Send Tutor mailing list submissions to
tutor at python.org
To subscribe or unsubscribe via the World Wide Web, visit
https://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-request at python.org
You can reach the person managing the list at
tutor-owner at python.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."
Today's Topics:
1. Re: Help!! Code ridden with Bugs (Joel Goldstick)
2. Re: Help!! Code ridden with Bugs (Sri Kavi)
3. Re: Help!! Code ridden with Bugs (Eloka Chima)
On Thu, Mar 9, 2017 at 8:28 AM, Eloka Chima via Tutor <tutor at python.org> wrote:
> I am new to programming but an immersive study in the past few weeks have brought me to speed so I can play around with codes but not really mastered them.My assignment below is ridden with bugs and I've done so much to get rid of them but to no avail.
> Below is the project :
>
> Create a class called ShoppingCart.
>
> Create a constructor that takes no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items.
>
> Create a method add_item that requires item_name, quantity and price arguments. This method should add the cost of the added items to the current value of total. It should also add an entry to the items dict such that the key is the item_name and the value is the quantity of the item.
>
> Create a method remove_item that requires similar arguments as add_item. It should remove items that have been added to the shopping cart and are not required. This method should deduct the cost of the removed items from the current total and also update the items dict accordingly.
>
> If the quantity of an item to be removed exceeds the current quantity of that item in the cart, assume that all entries of that item are to be removed.
>
> Create a method checkout that takes in cash_paid and returns the value of balance from the payment. If cash_paid is not enough to cover the total, return "Cash paid not enough".
>
> Create a class called Shop that has a constructor which takes no arguments and initializes an attribute called quantity at 100.
>
> Make sure Shop inherits from ShoppingCart.
>
> In the Shop class, override the remove_item method, such that calling Shop's remove_item with no arguments decrements quantity by one'''
>
>
>
>
> And below here is my code:
>
> class ShoppingCart(object):
>
> def __init__(self):
>
> self.items = {}
>
> self.total = 0
>
> def add_item(self,item_name,quantity,price):
>
> self.item_name = item_name
>
> self.quantity = quantity
>
> self.price = price
>
> if not self.item_name in self.items:
>
> self.items[self.item_name] = self.quantity
>
> self.total +=self.price
>
> def remove_item(self,item_name,quantity,price):
>
> self.item_name = item_name
>
> self.quantity = quantity
>
> self.price = price
>
> for self.item_name in self.items:
>
> if self.item_name in self.items:
>
> del self.items[self.item_name]
>
> self.total -= self.price
>
> if self.quantity > self.items:
>
> self.items = 0
>
> return self.items
>
> def checkout(self, cash_paid):
>
> self.cash_paid = cash_paid
>
> if self.cash_paid < self.total:
>
> self.total -= self.cash_paid
>
> return self.total
>
> return "Cash paid is not enough"
>
> class Shop(ShoppingCart):
>
> def __init__(self):
>
> self.quantity = 100
>
> def remove_item(self):
>
> self.quantity -= 1
>
> return self.quantity
>
>
>
>
> Below is the tests:
>
>
>
>
> import unittest
>
>
>
>
> class ShoppingCartTestCases(unittest.TestCase):
>
> def setUp(self):
>
> self.cart = ShoppingCart()
>
> self.shop = Shop()
>
>
>
> def test_cart_property_initialization(self):
>
> self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
>
> self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
>
>
>
> def test_add_item(self):
>
> self.cart.add_item('Mango', 3, 10)
>
>
>
> self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
>
> self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
>
>
>
> def test_remove_item(self):
>
> self.cart.add_item('Mango', 3, 10)
>
> self.cart.remove_item('Mango', 2, 10)
>
>
>
> self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
>
> self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
>
>
>
> def test_checkout_returns_correct_balance(self):
>
> self.cart.add_item('Mango', 3, 10)
>
> self.cart.add_item('Orange', 16, 10)
>
>
>
> self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
>
> self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
>
>
>
> def test_shop_is_instance_of_shopping_cart(self):
>
> self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
>
>
>
>
> def test_shop_remove_item_method(self):
>
> for i in range(15):
>
> self.shop.remove_item()
>
>
>
>
>
>
> self.assertEqual(self.shop.quantity, 85)
Welcome.
Two things: First, format your code correctly, it is double spaced.
Second, what exactly is wrong with your code? If it runs, but outputs
the wrong values, point that out. If it fails to run, copy and paste
the complete traceback so that others can identify where you have gone
wrong.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
def checkout(self, cash_paid):
self.cash_paid = cash_paid
if self.cash_paid < self.total:
self.total -= self.cash_paid
return self.total
return "Cash paid is not enough"
You cannot return two values.
Sri
On Thu, Mar 9, 2017 at 6:58 PM, Eloka Chima via Tutor <tutor at python.org>
wrote:
> I am new to programming but an immersive study in the past few weeks have
> brought me to speed so I can play around with codes but not really mastered
> them.My assignment below is ridden with bugs and I've done so much to get
> rid of them but to no avail.
> Below is the project :
>
> Create a class called ShoppingCart.
>
> Create a constructor that takes no arguments and sets the total attribute
> to zero, and initializes an empty dict attribute named items.
>
> Create a method add_item that requires item_name, quantity and price
> arguments. This method should add the cost of the added items to the
> current value of total. It should also add an entry to the items dict such
> that the key is the item_name and the value is the quantity of the item.
>
> Create a method remove_item that requires similar arguments as add_item.
> It should remove items that have been added to the shopping cart and are
> not required. This method should deduct the cost of the removed items from
> the current total and also update the items dict accordingly.
>
> If the quantity of an item to be removed exceeds the current quantity of
> that item in the cart, assume that all entries of that item are to be
> removed.
>
> Create a method checkout that takes in cash_paid and returns the value of
> balance from the payment. If cash_paid is not enough to cover the total,
> return "Cash paid not enough".
>
> Create a class called Shop that has a constructor which takes no arguments
> and initializes an attribute called quantity at 100.
>
> Make sure Shop inherits from ShoppingCart.
>
> In the Shop class, override the remove_item method, such that calling
> Shop's remove_item with no arguments decrements quantity by one'''
>
>
>
>
> And below here is my code:
>
> class ShoppingCart(object):
>
> def __init__(self):
>
> self.items = {}
>
> self.total = 0
>
> def add_item(self,item_name,quantity,price):
>
> self.item_name = item_name
>
> self.quantity = quantity
>
> self.price = price
>
> if not self.item_name in self.items:
>
> self.items[self.item_name] = self.quantity
>
> self.total +=self.price
>
> def remove_item(self,item_name,quantity,price):
>
> self.item_name = item_name
>
> self.quantity = quantity
>
> self.price = price
>
> for self.item_name in self.items:
>
> if self.item_name in self.items:
>
> del self.items[self.item_name]
>
> self.total -= self.price
>
> if self.quantity > self.items:
>
> self.items = 0
>
> return self.items
>
> def checkout(self, cash_paid):
>
> self.cash_paid = cash_paid
>
> if self.cash_paid < self.total:
>
> self.total -= self.cash_paid
>
> return self.total
>
> return "Cash paid is not enough"
>
> class Shop(ShoppingCart):
>
> def __init__(self):
>
> self.quantity = 100
>
> def remove_item(self):
>
> self.quantity -= 1
>
> return self.quantity
>
>
>
>
> Below is the tests:
>
>
>
>
> import unittest
>
>
>
>
> class ShoppingCartTestCases(unittest.TestCase):
>
> def setUp(self):
>
> self.cart = ShoppingCart()
>
> self.shop = Shop()
>
>
>
> def test_cart_property_initialization(self):
>
> self.assertEqual(self.cart.total, 0, msg='Initial value of total not
> correct')
>
> self.assertIsInstance(self.cart.items, dict, msg='Items is not a
> dictionary')
>
>
>
> def test_add_item(self):
>
> self.cart.add_item('Mango', 3, 10)
>
>
>
> self.assertEqual(self.cart.total, 30, msg='Cart total not correct
> after adding items')
>
> self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items
> not correct after adding item')
>
>
>
> def test_remove_item(self):
>
> self.cart.add_item('Mango', 3, 10)
>
> self.cart.remove_item('Mango', 2, 10)
>
>
>
> self.assertEqual(self.cart.total, 10, msg='Cart total not correct
> after removing item')
>
> self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items
> not correct after removing item')
>
>
>
> def test_checkout_returns_correct_balance(self):
>
> self.cart.add_item('Mango', 3, 10)
>
> self.cart.add_item('Orange', 16, 10)
>
>
>
> self.assertEqual(self.cart.checkout(265), 75, msg='Balance of
> checkout not correct')
>
> self.assertEqual(self.cart.checkout(25), 'Cash paid not enough',
> msg='Balance of checkout not correct')
>
>
>
> def test_shop_is_instance_of_shopping_cart(self):
>
> self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not
> a subclass of ShoppingCart')
>
>
>
>
> def test_shop_remove_item_method(self):
>
> for i in range(15):
>
> self.shop.remove_item()
>
>
>
>
>
>
> self.assertEqual(self.shop.quantity, 85)
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
Your Code Solution Has Errors
THERE IS AN ERROR/BUG IN YOUR CODE
Results: Traceback (most recent call last): File "python/nose2/bin/nose2", line 8, in discover() File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 300, in discover return main(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 100, in __init__ super(PluggableTestProgram, self).__init__(**kw) File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__ self.parseArgs(argv) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 133, in parseArgs self.createTests() File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 258, in createTests self.testNames, self.module) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 67, in loadTestsFromNames for name in event.names] File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 82, in loadTestsFromName result = self.session.hooks.loadTestsFromName(event) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/events.py", line 224, in __call__ result = getattr(plugin, self.method)(event) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/plugins/loader/testclasses.py", line 119, in loadTestsFromName result = util.test_from_name(name, module) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 106, in test_from_name parent, obj = object_from_name(name, module) File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 117, in object_from_name module = __import__('.'.join(parts_copy)) File "/home/ubuntu/Applications/andelabs-server/tmp/58b15152d171bb1a00a61531-586ba236d394751500fff7e4-test.py", line 4, in from tmp.andelabs_58b15152d171bb1a00a61531_586ba236d394751500fff7e4 import * File "/home/ubuntu/Applications/andelabs-server/tmp/andelabs_58b15152d171bb1a00a61531_586ba236d394751500fff7e4.py", line 17 if not self.item_name in self.items: ^IndentationError: unexpected indentI get this error when I run the code. After editing the code I sent. Thanks.
Sent from Yahoo Mail on Android
On Thu, 9 Mar 2017 at 6:26 pm, Sri Kavi<gvmcmt at gmail.com> wrote:
def checkout(self, cash_paid):
self.cash_paid = cash_paid
if self.cash_paid < self.total:
self.total -= self.cash_paid
return self.total
return "Cash paid is not enough"
You cannot return two values.
Sri
On Thu, Mar 9, 2017 at 6:58 PM, Eloka Chima via Tutor <tutor at python.org> wrote:
I am new to programming but an immersive study in the past few weeks have brought me to speed so I can play around with codes but not really mastered them.My assignment below is ridden with bugs and I've done so much to get rid of them but to no avail.
Below is the project :
Create a class called ShoppingCart.
Create a constructor that takes no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items.
Create a method add_item that requires item_name, quantity and price arguments. This method should add the cost of the added items to the current value of total. It should also add an entry to the items dict such that the key is the item_name and the value is the quantity of the item.
Create a method remove_item that requires similar arguments as add_item. It should remove items that have been added to the shopping cart and are not required. This method should deduct the cost of the removed items from the current total and also update the items dict accordingly.
If the quantity of an item to be removed exceeds the current quantity of that item in the cart, assume that all entries of that item are to be removed.
Create a method checkout that takes in cash_paid and returns the value of balance from the payment. If cash_paid is not enough to cover the total, return "Cash paid not enough".
Create a class called Shop that has a constructor which takes no arguments and initializes an attribute called quantity at 100.
Make sure Shop inherits from ShoppingCart.
In the Shop class, override the remove_item method, such that calling Shop's remove_item with no arguments decrements quantity by one'''
And below here is my code:
class ShoppingCart(object):
def __init__(self):
self.items = {}
self.total = 0
def add_item(self,item_name, quantity,price):
self.item_name = item_name
self.quantity = quantity
self.price = price
if not self.item_name in self.items:
self.items[self.item_name] = self.quantity
self.total +=self.price
def remove_item(self,item_name, quantity,price):
self.item_name = item_name
self.quantity = quantity
self.price = price
for self.item_name in self.items:
if self.item_name in self.items:
del self.items[self.item_name]
self.total -= self.price
if self.quantity > self.items:
self.items = 0
return self.items
def checkout(self, cash_paid):
self.cash_paid = cash_paid
if self.cash_paid < self.total:
self.total -= self.cash_paid
return self.total
return "Cash paid is not enough"
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
return self.quantity
Below is the tests:
import unittest
class ShoppingCartTestCases( unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
self.shop = Shop()
def test_cart_property_ initialization(self):
self.assertEqual(self.cart. total, 0, msg='Initial value of total not correct')
self.assertIsInstance(self. cart.items, dict, msg='Items is not a dictionary')
def test_add_item(self):
self.cart.add_item('Mango', 3, 10)
self.assertEqual(self.cart. total, 30, msg='Cart total not correct after adding items')
self.assertEqual(self.cart. items['Mango'], 3, msg='Quantity of items not correct after adding item')
def test_remove_item(self):
self.cart.add_item('Mango', 3, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart. total, 10, msg='Cart total not correct after removing item')
self.assertEqual(self.cart. items['Mango'], 1, msg='Quantity of items not correct after removing item')
def test_checkout_returns_correct_ balance(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart. checkout(265), 75, msg='Balance of checkout not correct')
self.assertEqual(self.cart. checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
def test_shop_is_instance_of_ shopping_cart(self):
self.assertTrue(isinstance( self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
def test_shop_remove_item_method( self):
for i in range(15):
self.shop.remove_item()
self.assertEqual(self.shop. quantity, 85)
______________________________ _________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/ mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor at python.org
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list