我正在使用 OOP 進行簡單的“賬單拆分”情況,出于某種原因,該專案的“磁區價格”(每當更多用戶投入該專案時更新;例如:1 人想要 5 美元的專案 = 5 美元的磁區價格,2 人想要它 = 2.5 美元的磁區價格,...)。
出于某種原因,Participant 中的“欠款”和 Item 中的 partition_price 不想更新,我不知道出了什么問題。
class Participant:
#Participant has a name and has items he wants to pitch in for
def __init__(self, name):
self.name = name
self.items = []
self.owes = []
#Append the item (Item class) to the item list
#Append participant's name to the item's participant list
#Append the item's partition price $ to the owes list
def add_Item(self, item):
self.items.append(item)
item.participants.append(self.name)
self.owes.append(item.partition_price)
class Item:
#Item has a name and a price
def __init__(self, name, price):
self.name = name
self.price = price
#List of participants that are pitching in for the item
self.participants = []
#If the list of participants is empty (no participants)
if not self.participants:
#The cost of a "pitch in" for this item is $0
self.partition_price = 0
#Otherwise, the cost of a "pitch in" for this item is its price/number of participants
else:
self.partition_price = price/len(self.participants)
#Create 2 people
matthew = Participant('Matthew')
bob = Participant('Bob')
#Create an item
juice = Item('Apple Juice', 2.99)
#These 2 people wants that item
matthew.add_Item(juice)
bob.add_Item(juice)
#Test
print(f'Matthew wants: {[i.name for i in matthew.items]}')
print(f'Who wants Apple Juice: {juice.participants}')
print(f'Matthew owes: {matthew.owes}')
print(f'The juice costs: {juice.price}, so each person owes: {juice.partition_price} <-- class function')
print(f'The juice ocsts: {juice.price}, so each person owes: {juice.price/len(juice.participants)} <-- external function')
這是它列印的內容:
Matthew wants: ['Apple Juice']
Who wants Apple Juice: ['Matthew', 'Bob']
Matthew owes: [0]
The juice costs: 2.99, so each person owes: 0 <-- class function
The juice ocsts: 2.99, so each person owes: 1.495 <-- external function
Matthew 在第 3 次印刷宣告中應欠 1.495。第 4 個列印陳述句也應該列印 1.495。只有外部功能起作用。
uj5u.com熱心網友回復:
您的 partition_price 是Item
在您第一次在線創建果汁的初始化期間計算的juice = Item('Apple Juice', 2.99),participants這將是一個空串列,從而導致partition_price存在0。
您隨后呼叫了add_Item函式matthew并將bob它們添加為參與者juice,這解釋了為什么juice.price/len(juice.participants)回傳 1.495
您可能希望有一個add_participant方法,Item而不是呼叫item.participants.append(self.name)它將參與者添加到self.participants并計算self.partition_price
class Participant:
#Participant has a name and has items he wants to pitch in for
def __init__(self, name):
self.name = name
self.items = []
self.owes = []
#Append the item (Item class) to the item list
#Append participant's name to the item's participant list
#Append the item's partition price $ to the owes list
def add_Item(self, item):
self.items.append(item)
item.add_participant(self.name)
self.owes.append(item.partition_price)
class Item:
#Item has a name and a price
def __init__(self, name, price):
self.name = name
self.price = price
#List of participants that are pitching in for the item
self.participants = []
#If the list of participants is empty (no participants)
if not self.participants:
#The cost of a "pitch in" for this item is $0
self.partition_price = 0
#Otherwise, the cost of a "pitch in" for this item is its price/number of participants
else:
self.partition_price = price/len(self.participants)
def add_participant(self, name):
self.participants.append(name)
self.partition_price = self.price / len(self.participants)
#Create 2 people
matthew = Participant('Matthew')
bob = Participant('Bob')
#Create an item
juice = Item('Apple Juice', 2.99)
#These 2 people wants that item
matthew.add_Item(juice)
bob.add_Item(juice)
#Test
print(f'Matthew wants: {[i.name for i in matthew.items]}')
print(f'Who wants Apple Juice: {juice.participants}')
print(f'Matthew owes: {matthew.owes}')
print(f'The juice costs: {juice.price}, so each person owes: {juice.partition_price} <-- class function')
print(f'The juice ocsts: {juice.price}, so each person owes: {juice.price/len(juice.participants)} <-- external function')
輸出:
Matthew wants: ['Apple Juice']
Who wants Apple Juice: ['Matthew', 'Bob']
Matthew owes: [2.99]
The juice costs: 2.99, so each person owes: 1.495 <-- class function
The juice ocsts: 2.99, so each person owes: 1.495 <-- external function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484668.html
