[Tutor] Help!! Code ridden with Bugs

Eloka Chima chima_eloka at yahoo.com
Thu Mar 9 08:28:05 EST 2017

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.assertEqual(self.shop.quantity, 85)

More information about the Tutor mailing list