Hello,

I have this home work question to be solved on a cloud ide:

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.

Here's my code:

class ShoppingCart(object):
  def __init__(self):
    self.total = 0
    self.items = {}

  def add_item(self, item_name, quantity, price):
    self.total += (quantity * price)
    #self.items.update({item_name : quantity})
    self.items[item_name] = quantity

  def remove_item(self, item_name, quantity, price):
    if quantity > self.items[item_name]:
       self.total -= (self.items[item_name] * price)
       if item_name in self.items:
         del self.items[item_name]
    else:
      self.total -= (quantity * price)
      if item_name in self.items:
        del self.items[item_name]

  def checkout(self, cash_paid):
    if cash_paid < self.total:
      return "Cash paid not enough"
    else:
     self.total = cash_paid - self.total
     return self.total

class Shop(ShoppingCart):
  def __init__(self):
    self.quantity = 100

  def remove_item(self):
    self.quantity -= 1

I get a KeyError each time in run the code. Any help will be greatly appreciated. Thanks.

Recommended Answers

All 4 Replies

This code does not throw a KeyError, it only defines two classes and does nothing.

Apparently it does. I can't figure it out. Regarding the code doing nothing, I didn't create any objects because there are test cases that are to be run against the code. Here's the test file:

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)

Can you see any errors with the code using this tests? Thank you

I get

Traceback (most recent call last):
  File "spec.py", line 24, in test_remove_item
    self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
KeyError: 'Mango'

If you look at the above code it means that after adding 3 mango items in the shopping cart and removing 2 such items, there is no Mango item left. There is a logical error in the add_item() and remove_item() algorithms. When you del self.items[item_name] it means that you remove all items with that name from the shopping cart. This is not what you want.

hey tried to get a solution but also gives an error.

class ShoppingCart:

   items = {}
   def __init__(self, total = 0):
       self.total = total
   def add_item(self, item_name, quantity, price):
       self.item_name = item_name
       self.quantity = quantity
       self.price = price
       if not item_name in self.items:
           self.items[item_name] = quantity
       total = self.price*self.quantity + self.total
       self.items[self.item_name] = self.quantity
   def remove_item(self, item_name, quantity, price):
       self.item_name = item_name
       self.quantity = quantity
       self.price = price
       total = self.total - price*quantity
       if item_name in self.items:
           del self.items[item_name]
           return self.items
   def checkout(self, cash_paid):
       self.cash_paid = cash_paid
       return self.total - cash_paid
       if cash_paid < self.total:
           return "Cash paid not enough"
class Shop(ShoppingCart):
   def __init__(self):
       self.quantity = 100

   def remove_item(self, quantity):
       self.quantity -= quantity
       return self.quantity
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.