我正在完成关于CodeAcademy的Python课程(我在我的计算机上运行代码而不是CodeAcademy)并编写了这段代码来将项目添加到购物车。购物车是一本字典。
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)
您可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."
这将压缩product
和price
列表,并从两个列表中一次提供一个值。所以我们将得到产品及其相应的价格。
或者您可以迭代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."
答案 1 :(得分: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):
答案 2 :(得分:0)
您正在将items_in_cart [product]的值设置为price,即字符串列表。您应该将其设置为价格中的一个字符串。
这是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
答案 3 :(得分: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