Python购物车添加到购物车,总计获得num件商品 [英] Python shopping cart add to cart, get total get num items
问题描述
我正在为期末考试而学习,这是我错过的测验问题。我需要有关getTotal方法的大部分帮助。我需要遍历列表,找到每个项目的价格,将价格添加到总计中并返回总计。我在循环中挣扎,我不确定如何将第二个项目从列表中拉出。[1]我尝试了很多方法,并且感到沮丧。
I am studying for my final and this was a quiz question I missed. I need most of the help on the getTotal method. I need to loop through the list, find the price of each item, add the price to the total and return the total. I struggle with loops and I am not sure how to pull the second item out of a list.. [1] ?? I have tried many ways and am getting frustrated.
如果有人愿意帮助我,那就太好了。我仍然在学习并且对此很陌生,所以请放轻松,但我真的很想学习。它可能不像我说的那么难,但是我会等待一些输入。谢谢!
If there is anyone up that is willing to help me that would be great. I am still learning and am new at this so go easy on me, but I really want to learn it. It's probably not as hard as I make it out to be, but Ill be waiting for some input. Thank you!
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
def getPrice(self):
return self.price
def getName(self):
return self.name
class Cart:
def __init__(self, list):
self.list = []
def addItem(self, item):
self.list.append(self.list)
def getTotal(self):
total = 0
for item in self.list:
name, price = item # or price = item[1]
total = total + price
def getNumItems(self):
count = 0
for c in range(self.list):
count = self.list + 1
return count
def removeItem(self, item)
#removes the item from the cart's item list
def main():
item1 = Item("Banana", .69)
item2 = Item("Eggs", 2.39)
item3 = Item("Donut", .99)
c = Cart()
c.addItem(item1)
c.addItem(item2)
c.addItem(item3)
print "You have %i items in your cart for a total of $%.02f" %(c.getNumItems(), c.getTotal())
c.removeItem(item3)
print "You have %i items in your cart for a total of $%.02f" % (c.getNumItems(), c.getTotal())
main()
推荐答案
在这里花了点时间,我更改了代码,现在它可以正常使用了购物车
Here gives the time and I changed the code and now it is fully functional shopping cart
class Item(object):
def __init__(self, unq_id, name, price, qty):
self.unq_id = unq_id
self.product_name = name
self.price = price
self.qty = qty
class Cart(object):
def __init__(self):
self.content = dict()
def update(self, item):
if item.unq_id not in self.content:
self.content.update({item.unq_id: item})
return
for k, v in self.content.get(item.unq_id).iteritems():
if k == 'unq_id':
continue
elif k == 'qty':
total_qty = v.qty + item.qty
if total_qty:
v.qty = total_qty
continue
self.remove_item(k)
else:
v[k] = item[k]
def get_total(self):
return sum([v.price * v.qty for _, v in self.content.iteritems()])
def get_num_items(self):
return sum([v.qty for _, v in self.content.iteritems()])
def remove_item(self, key):
self.content.pop(key)
if __name__ == '__main__':
item1 = Item(1, "Banana", 1., 1)
item2 = Item(2, "Eggs", 1., 2)
item3 = Item(3, "Donut", 1., 5)
cart = Cart()
cart.update(item1)
cart.update(item2)
cart.update(item3)
print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
cart.remove_item(1)
print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
输出为:
You have 8 items in your cart for a total of $8.00
You have 7 items in your cart for a total of $7.00
这篇关于Python购物车添加到购物车,总计获得num件商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!