2014-12-25 151 views
1

我正在完成CodeAcademy上的Python課程(我在我的計算機上運行的代碼不是CodeAcademy),並編寫了這段代碼以將項目添加到購物車。購物車是一本字典。Python:將多個產品和價格添加到購物車

class ShoppingCart(object): 

    """Creates shopping cart objects 
    for users of our fine website.""" 

    items_in_cart = {} 

    def __init__(self, customer_name): 
     self.customer_name = customer_name 

    def add_item(self, product, price): 
     """Add product to the cart.""" 
     if not product in self.items_in_cart: 
      self.items_in_cart[product] = price 
      print product + " added." 
     else: 
      print product + " is already in the cart." 

my_cart = ShoppingCart("Amy") 

my_cart.add_item("Nishiki Modulus", "$400") 

print my_cart.items_in_cart 

此代碼的工作和回報:

Nishiki Modulus added. 
{'Nishiki Modulus': '$400'} 

但我想弄清楚如何在同一時間添加多個項目(和價格)。我一直在試驗沒有運氣。

我跑

class ShoppingCart(object): 

    items_in_cart = {} 
    def __init__(self, customer_name): 
     self.customer_name = customer_name 

    def add_item(self, product, price): 
     """Add product to the cart.""" 
     for products in product: 
      if not products in self.items_in_cart: 
       self.items_in_cart[products] = price 
       print "added." 
      else: 
       print "Product is already in the cart." 

my_cart = ShoppingCart("Amy") 

my_cart.add_item(["Nishiki Modulus", "Trek 700", "Fuji Sportif"], ["$400", "$450", "$700"]) 

print my_cart.items_in_cart 

正如我預言,鍵是正確的,但不是值。這將返回:

added. 
added. 
added. 
{'Trek 700': ['$400', '$450', '$700'], 'Fuji Sportif': ['$400', '$450', '$700'],  'Nishiki Modulus': ['$400', '$450', '$700']} 

我想沿着線的東西:

for products, price in product.items(): 

但我無法弄清楚如何正確添加列表items_in_cart

任何人都可以點我在正確的方向?請讓我知道,如果有什麼不清楚。

+0

嘗試編寫清晰有意義的代碼。這將幫助你和其他人瞭解正在發生的事情,以及你的代碼的意圖是什麼。例如,「產品中的產品」是無意義的:「產品」是單個產品,「產品」是多個產品。 –

+0

謝謝約翰,我明白你在說什麼。我錯過了複數形式,這可能會讓人困惑。我將努力以更好的形式編寫我的代碼。 –

回答

0

可以zip的產品,其價格是這樣

for prod, money in zip(product, price): 
    if not prod in self.items_in_cart: 
     self.items_in_cart[prod] = money 
     print "added." 
    else: 
     print "Product is already in the cart." 

這將壓縮的productprice列表和來自這兩個名單時給予一個值。所以我們會得到產品和相應的價格。

或者你可以迭代product名單,並獲得來自price與指數對應的值,這樣

for index, prod in enumerate(product): 
    if not prod in self.items_in_cart: 
     self.items_in_cart[prod] = price[index] 
     print "added." 
    else: 
     print "Product is already in the cart." 
+0

謝謝!我還沒有遇到zip函數(非常瞭解Python和編程),但查看後,這正是我所需要的。 –

0

你可以用簡單的索引for環路嘗試:

def add_item(self, product, price): 
    """Add product to the cart.""" 
    for index in range(len(product): 
     if not product[index] in self.items_in_cart: 
      self.items_in_cart[product[index]] = price[index] 
      print "added." 
     else: 
      print "Product is already in the cart." 

或使用zip

for products, prices in zip(product, price): 
0

您正在將items_in_cart [product]的值設置爲價格,即字符串列表。您應該將其設置爲價格中的一個字符串。

這裏是固定的ShoppingCart類:

class ShoppingCart(object): 

    items_in_cart = {} 
    def __init__(self, customer_name): 
     self.customer_name = customer_name 

    def add_item(self, product, price): 
     """Add product to the cart.""" 
     priceIndex = 0 
     for products in product: 
      if not products in self.items_in_cart: 
       self.items_in_cart[products] = price[priceIndex] 
       print "added." 

      else: 
       print "Product is already in the cart." 

      priceIndex += 1 
0

我想補充一個新的方法add_items是在循環中調用你原來add_item。這將使您的代碼更清晰,更易於使用。

class ShoppingCart(object): 

    items_in_cart = {} 
    def __init__(self, customer_name): 
     self.customer_name = customer_name 

    def add_item(self, product, price): 
     """Add product to the cart.""" 
     if not product in self.items_in_cart: 
      self.items_in_cart[product] = price 
      print product + " added." 
     else: 
      print product + " is already in the cart." 

    def add_items(self, products, prices): 
     for product, price in zip(products, prices): 
      self.add_item(product, price) 

my_cart = ShoppingCart("Amy") 

my_cart.add_items(["Nishiki Modulus", "Trek 700", "Fuji Sportif"], ["$400", "$450", "$700"]) 
print my_cart.items_in_cart 
相關問題