[Tutor] Help!! Code ridden with Bugs
Eloka Chima
chima_eloka at yahoo.com
Thu Mar 9 13:41:26 EST 2017
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
More information about the Tutor
mailing list