2012-12-14 87 views
0

我正在爲我的決賽進行學習,這是一個我錯過的測驗問題。我需要getTotal方法的大部分幫助。我需要遍歷列表,找到每件商品的價格,將價格加到總價中並返回總價。我與循環鬥爭,我不知道如何從列表中拉出第二項。[1] ??我嘗試了很多方法,並且感到沮喪。Python購物車加入購物車,獲取總數獲得num項目

如果有人願意幫助我,那會很棒。我仍然在學習,對此我很新,所以對我來說很容易,但我真的很想學習它。這可能並不像我想要的那樣艱難,但是我會等待一些輸入。謝謝!

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() 
+0

要通過項目循環,你做'的項目在self.list中:'。爲了得到物品的價格,你可以做'item.price'。因此,創建一個名爲'total'的變量,將其設置爲'0',併爲列表中的每個項目增加一個變量。 –

+0

我看不到你的整個評論,除非我發表評論,所以請大聲笑 – Nick

回答

1

這裏給出了時間,我改變了代碼,現在它是全功能的購物車

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 
1

爲getTotal:

def getTotal(self): 
    total = 0 
    for item in self.list: 
     name, price = item # or price = item[1] 
     total = total + price 

順便說一句,你的addItem和getNumItems方法是也是錯誤的。既然它是最終的,你應該試着瞭解你在做什麼。

+0

這就是我想要做的就是理解它:) – Nick