简体   繁体   中英

Trouble with removing items from list

I'm having trouble getting this function to work. The purpose of it is to delete items from a list.

def sell(inventory_list):
    print()
    count = int(input('How many items would you like to sell? '))
    print()

    for count in range(count):
        print()
        type = input('Enter the type of item you wish to sell? ')
        print()
        name = input('Enter the name of the item you wish to sell? ')
        print()
        price = float(input('What is the price of the item you wish to sell? $'))

        items = Plant.SubPlant(type, name, price)

        inventory_list.remove(items)

    return inventory_list

Your inventory list doesn't have the new instance you are trying to remove. Just because it contains the same atrrs/values does not mean they are the same.

To be able to do this perhaps implement method __eq__ in your SubPlant class:

class SubPlant(object):
    def __init__(self, type, name, price):
        self.type = type
        self.name = name
        self.price = price

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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