2016-05-04 7 views
-1

この機能を使用するには問題があります。その目的は、リストから項目を削除することです。リストから項目を削除する際に問題が発生する

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

'inventory_list.remove(項目)は'あなたが返す可変オブジェクトを、変異します。どのくらい正確にこの関数を呼び出していますか? – TigerhawkT3

+0

print()は引数を取るべきです、何を印刷しようとしていますか?あなたはcount変数を再利用しています - おそらく "範囲(count)のxに対して" – flyingmeatball

+0

@flyingmeatballそれは新しい行を表示しています –

答えて

3

あなたの在庫リストには、削除しようとしている新しいインスタンスがありません。同じatrrs /値を含んでいるからといって、同じものであるとは限りません。

これはおそらくあなたのSubPlantクラスのメソッドを実装__eq__行うことができるようにするには:

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__ 
関連する問題