我目前Python中的任務,我需要這方面的幫助,我到購物車功能在Python
創建一個名爲ShoppingCart類。
創建一個沒有參數並將total屬性設置爲零的構造函數,並初始化一個名爲items的空dict屬性。
創建一個方法add_item,它需要item_name,quantity和price參數。此方法應將添加項目的成本添加到當前總值中。它還應該添加一個項目字典的條目,這樣的關鍵是item_name和值是項目的數量。
創建一個方法remove_item,它需要與add_item類似的參數。它應該刪除已添加到購物車並且不需要的商品。這種方法應該從當前總額中扣除這些項目的成本,並相應地更新項目字典。如果要移除的物品數量超過購物車中的當前數量,則假定該物品的所有條目都將被移除。
創建一個方法checkout,它接受cash_paid並從付款中返回餘額值。如果cash_paid不足以支付總額,則返還現金支付不足。
創建具有在100
初始化名爲量的屬性,確保從店繼承的購物構造一類叫做店。
在Shop類中,重寫remove_item方法,例如調用Shop的remove_item(不帶參數)數量減一。
下面是本
import unittest;
class Test(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_add_item_hidden(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart.total, 190, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Orange'], 16, 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_remove_item_hidden(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 170, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 160, msg='Cart total not correct after removing item')
with self.assertRaises(KeyError):
self.cart.items['Mango']
def test_checkout_returns_correct_value(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_initializaton(self):
self.assertEqual(self.shop.quantity, 100, msg='Shop quantity not initialized correctly')
def test_shop_remove_item_method(self):
for i in range(15):
self.shop.remove_item()
self.assertEqual(self.shop.quantity, 85)
樣品測試代碼我試圖與下面
class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = {}
def add_item(self,item_name,quantity,price):
self.items[item_name] = quantity
self.total += price*quantity
def remove_item(self,item_name,quantity,price):
if quantity < self.items[item_name] and quantity >= 0:
self.total -= price*quantity
self.items[item_name] -= quantity
elif quantity >= self.items[item_name] :
del self.items[item_name]
def checkout(self,cash_paid):
if cash_paid >= self.total:
return cash_paid - self.total
else:
return "Cash paid not enough"
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
代碼的所有其他任務都解決了,但錯誤我回來的回報的時候解決這個我運行測試是
test_remove_item_hidden
170 != 160 : Cart total not correct after removing item
我似乎無法找到問題。請任何幫助將考慮我是新來的蟒蛇。提前致謝。
此聲明'quantity
@SpencerWieczorek我不明白你的意思 – diagold