简体   繁体   English

如何修改列表中项目的数量?

[英]How do I modify the quantity of an item in a list?

I have to write a code that modifies the quantity of an item in a shopping cart.我必须编写一个代码来修改购物车中商品的数量。 Initially, I have a class names ItemToPurhcase that takes in the item's name, quantity, price and description.最初,我有一个 class 名称 ItemToPurhcase ,其中包含项目的名称、数量、价格和描述。 Under ShoppingCart class, function modify_item() modifies an item's quantity.在 ShoppingCart class、function modify_item() 下修改商品的数量。 Has a parameter of type ItemToPurchase.具有 ItemToPurchase 类型的参数。 Does not return anything.不返回任何东西。 If item can be found (by name) in cart, modify item in cart.如果可以在购物车中找到商品(按名称),请修改购物车中的商品。 If item cannot be found (by name) in cart, output this message: Item not found in cart.如果在购物车中找不到项目(按名称),output 此消息:在购物车中找不到项目。 Nothing modified.没有任何修改。 My code so far:到目前为止我的代码:

class ItemToPurchase:
    def __init__(self, name="none", price=0, quantity=0, descrip='none'):
        self.name = name
        self.quantity = quantity
        self.price = price
        self.descrip = descrip

    def print_item_cost():
        print(self.name + ' ' + str(self.quantity) + '@ $' + str(self.price) + '= $' + str(self.price * self.quantity))

    def print_item_description(ItemToPurchase):
        print(self.name + ':' + str(self.descrip)

class ShoppingCart:
    def __init__(self, customer_name='none', current_date='January 1, 2016', cart_items):
        self.customer_name = customer_name
        self.currend_date = current_date
        self.cart_items = []

    def add_item(self, ItemToPurchase):
        self.cart_items.append(ItemToPurchase)
        
    def remove_item(self, item_name):
        while item_name in self.cart_items:
            del self.cart_items[item_name]
        if item_name not in self.cart_items:
            print('Item not found in cart. Nothing removed')

    def modify_item(self, ItemToPurchase):
        if name in self.cart_items:

I need help completing the function.我需要帮助来完成 function。

The shopping cart doesn't contain names, it contains ItemToPurchase .购物车不包含名称,它包含ItemToPurchase So you can't just check if the name is in the list, you have to check whether any of the items in the list has a .name attribute that matches.因此,您不能只检查名称是否在列表中,您必须检查列表中的任何项目是否具有匹配的.name属性。

The modify_item function needs to take the new quantity as a parameter. modify_item function 需要以新的数量为参数。 Then it can loop through the cart items and update the quantity.然后它可以遍历购物车项目并更新数量。

def remove_item(self, item_name):
    old_len = len(self.cart_items)
    self.cart_items = [item for item in self.cart_items if item.name != item_name]
    if len(self.cart_items) == old_len:
        print('Item not found in cart. Nothing removed')

def modify_item(self, item_name, new_quantity):
    for item in enumerate(cart_items):
        if item.name == item_name:
            item.quantity = new_quantity
            break
    else:
        print("Item not found in cart. Nothing modified")

I would make your code look like this:我会让你的代码看起来像这样:

def modify_item(self, item: ItemToPurchase):
    item_names = [item.name for item in self.cart_items]
    if item.name in item_names:
        spot = item_names.index(item.name)
        self.cart_items[spot] = item
    else:
        return "Item not found in cart. Nothing modified."

The item parameter takes an instance of the ItemToPurchase Class, and then the function uses list comprehension to find the names of all of the items in the cart. item 参数采用 ItemToPurchase Class 的实例,然后 function 使用列表解析来查找购物车中所有商品的名称。 Then it checks if the name of the item to be modified is already in the cart before it finds what spot in the list that item is at and then changes the item.然后它检查要修改的项目的名称是否已经在购物车中,然后再找到该项目在列表中的哪个位置,然后更改该项目。 Otherwise, it returns "Item not found in cart. Nothing modified."否则,它会返回“购物车中未找到商品。没有任何修改。”

I also edited the rest of your code to get rid of the rest of the bugs:我还编辑了代码的 rest 以消除错误的 rest:

class ItemToPurchase:
    def __init__(self, name="none", price=0, quantity=0):
        self.name = name
        self.quantity = quantity
        self.price = 0
        self.description = 'none'

    def set_description(self, descrip):
        self.description = descrip

    def print_item_description(self):
        print(self.name + ':' + str(self.description))

    def print_item_cost(self):
        print(self.name + ' ' + str(self.quantity) + '@ $' + str(self.price) + '= $' + str(self.price * self.quantity))


class ShoppingCart:
    def __init__(self, cart_items: list, customer_name='none', current_date='January 1, 2016'):
        self.customer_name = customer_name
        self.current_date = current_date
        self.cart_items = cart_items

    def add_item(self, new_item):
        self.cart_items.append(new_item)

    def remove_item(self, item_name):
        item_names = [item.name for item in self.cart_items]
        if item_name in item_names:
            spot = item_names.index(item_name)
            del self.cart_items[spot]
        if item_name not in self.cart_items:
            print('Item not found in cart. Nothing removed')

    def modify_item(self, item: ItemToPurchase):
        item_names = [item.name for item in self.cart_items]
        if item.name in item_names:
            spot = item_names.index(item.name)
            self.cart_items[spot] = item
        else:
            return "Item not found in cart. Nothing modified."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM