現在作業中です。クラスとオブジェクトのタプルエラー
クラスカーをデザインしています。 このクラスには次の属性があります。メーカー、モデル、価格 Carクラスには、これらの属性の現在の値を返すメソッドが必要です。さらに、このクラスには、価格属性値を変更するupdate_priceメソッドがあります。
ディーラーと呼ばれるクラスを設計します。 このクラスには、アドレスとインベントリという属性があります。 inventory属性は、Carオブジェクトのリストです。 ディーラークラスには、インベントリにcarオブジェクトを追加するメソッド、インベントリからcarオブジェクトを削除するメソッド(車のモデルに基づいて車の削除を行う必要があります)、および特定の車種の価格を更新するメソッドが必要です。このクラスには、インベントリ全体を表示するメソッドと、インベントリ内のすべての車の合計値を計算するメソッドもあります。
メイン機能では、プログラムはディーラーオブジェクトを作成し、複数の車を在庫に追加し、現在の在庫を表示します。次に、プログラムは在庫から車モデルを取り除き、所定の車モデルの価格を更新する。最後に、インベントリ全体と、現在インベントリ内にあるすべての車の合計値が表示されます。
しかし、私は自分のコードを実行したとき、私は
TypeError: descriptor 'append' requires a 'list' object but received a 'tuple'
を取得し、私はそのエラーが発生しているところはどこでも、あなたが何かを追加しようとしているようこの
class Car:
def __init__(self, maker, model, price):
self.__maker=maker
self.__model=model
self.__price=price
def get_model(self):
return self.__model
def get_maker(self):
return self.__maker
def get_price(self):
return self.__price
def create_list(list):
maker=input('Enter Car maker: ')
model=input('Enter Car model: ')
price=float(input('Enter Car price: $'))
account=(maker, model, price)
list.append(account)
def update_price(self):
self.__price=price
return self.__update_price
class Dealer():
def __init__(address, inventory):
self.__inventory=inventory
self.__address=address
def update_price(list):
model=input('Enter car model: ')
account=Car(maker, model, price)
found='false'
for model in list:
if account.get_model() == model:
account.append(price)
found=='true'
if found == 'false':
print('Model Not Found')
return account
def show_inventory(list):
show_inventory=print(account)
def calculate_total_value(list):
print(sum(self.__account.price))
def remove_car(list):
model=input('Model for removal')
found = 'false'
for model in list:
if account.get_model() ==model:
account.remove(model)
found =='true'
if found =='false':
print('Model Not Found')
def get_address(address):
self.__address=address
return self.__address
def main():
address=input('Enter address: ')
account_list=[]
counter=1
manytimes=float(input('How many cars to add? '))
while counter<=manytimes:
counter=counter+1
create_list(list)
show_inventory(account_list)
remove_car(account_list)
show_inventory(list)
update_price(account_list)
show_inventory(account_list)
calculate_total_value(account_list)
main()
実際のリストの代わりに 'list'型を' create_list'に渡しました。 – user2357112
[タプルに値を追加する方法は?](https://stackoverflow.com/questions/4913397/how-to-add-value-to-a-tuple) – alfasin
[mcve]を作成する方法を参照してください。 –