0

HELP ME ...............OBJECT ORIENTED PROGRAMMING Create a class called ShoppingCart

could some one tell me whats wrong with my code especially the function remove_item class ShoppingCart(object): def __init__(self,total=0,items = {} ): self.total = total self.items = items def add_item(self,item_name="",quantity =0,price = 0): self.total += price*quantity self.items = {item_name:quantity} def remove_item(self, item_name="", quantity =0, price=0): self.total -= price*quantity if self.items[item_name] <= quantity: del self.items else: self.items[item_name] -= quantity def checkout(self,cash_paid): balance = 0 if cash_paid < self.total: return "Cash Paid Not Enough" else: balance = cash_paid -self.total return balance class Shop(ShoppingCart): def __init__(self,quantity=100): self.quantity = quantity def remove_item(self): self.quantity -= 1

6th Apr 2017, 10:31 PM
Christopher eleison
6 ответов
+ 12
What's the problem exactly? I tried this code in the playground and it ran without errors.
6th Apr 2017, 8:52 PM
Michael Foster
Michael Foster - avatar
7th Apr 2017, 1:14 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 7
We can help you better, if you present your code. Sugestion: Post your code in the Code Playground. Then come back and tell us its name.
7th Apr 2017, 11:32 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 6
checkout my test with your code: (I comented the points I changed and why) https://code.sololearn.com/cm1Si2NfqrwS/?ref=app
7th Apr 2017, 1:04 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
thank you Cruz wel it passed all the tests but am getting this error i really wonder why THERE IS AN ERROR/BUG IN YOUR CODE Here is the relevant part of the stack trace: KeyError('Mango',),
7th Apr 2017, 8:51 AM
Christopher eleison
- 1
the problem is on the checkout method i get this error 1 . test_checkout_returns_correct_balance Failure in line 35, in test_checkout_returns_correct_balance self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct') AssertionError: Balance of checkout not correc after the unitest 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)
6th Apr 2017, 9:17 PM
Christopher eleison