oop - Object Oriented programming in python -


i'm trying hands @ exercises, came 1 :

create class called shoppingcart.

create constructor takes no arguments , sets total attribute zero, , initializes empty dict attribute named items.

create method add_item requires item_name, quantity , price arguments. method should add cost of added items current value of total. should add entry items dict such key item_name , value quantity of item.

create method remove_item requires similar arguments add_item. should remove items have been added shopping cart , not required. method should deduct cost of removed items current total , update items dict accordingly.

if quantity of item removed exceeds current quantity of item in cart, assume entries of item removed.

create method checkout takes in cash_paid , returns value of balance payment. if cash_paid not enough cover total, return "cash paid not enough".

create class called shop has constructor takes no arguments , initializes attribute called quantity @ 100.

make sure shop inherits shoppingcart.

in shop class, override remove_item method, such calling shop's remove_item no arguments decrements quantity one.

wrote (see below) , ran through tests (see below), passed tests, when trying submit on platform i'm taking exercise, returned error >>>> keyerror('mango',), cause of this? , how can solve it? if asses tests, doesn't mean code go ? i'm stuck

the 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 = {item_name : quantity}  def remove_item(self, item_name, quantity, price): self.total -= (quantity * price) if quantity >= self.items[item_name]:     del self.items[item_name] self.items[item_name] -= quantity  def checkout(self, cash_paid): balance = 0 if cash_paid < self.total:   return "cash paid not enough" balance = cash_paid - self.total return balance   class shop(shoppingcart):  def __init__(self): shoppingcart.__init__(self) self.quantity = 100  def remove_item(self): self.quantity -=1 

the test

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 not 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 not subclass of shoppingcart')  def test_shop_remove_item_method(self): in range(15):   self.shop.remove_item()  self.assertequal(self.shop.quantity, 85) 

the implementation of add_item incorrect.

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

this line:

    self.items = {item_name : quantity} 

should be:

    self.items[item_name] = self.items.get(item_name, 0) + quantity 

also in remove_item permit 1 remove more quantity there is, add items , remove them , free fruit:

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

should this:

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

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -