我用OOP做了一个简单的"账单拆分"情况,出于某种原因,项目的"分区价格"(每当有更多用户加入该项目时就会更新;例如:1个人想要一个5美元的项目=5美元的分区价格,2个人想要它=2.5美元的分区价格,…).

出于某种原因,Participant中的"owes"和Item中的partition\u 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

马修在第三次打印对账单时应支付1.495英镑.第四个打印语句也应打印1.495.只有外部功能起作用.

推荐答案

您的partition\u价格是在初始化Item期间计算的

随后为matthewbob调用add_Item函数,将它们添加为juice中的参与者,这解释了为什么juice.price/len(juice.participants)返回1.495

您可能希望在Item中使用add_participant方法,而不是调用item.participants.append(self.name),在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

Python相关问答推荐

pandas DataFrame GroupBy.diff函数的意外输出

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

在Python中管理打开对话框

使用@ guardlasses. guardlass和注释的Python继承

在含噪声的3D点网格中识别4连通点模式

Python Pandas获取层次路径直到顶层管理

判断solve_ivp中的事件

在pandas/python中计数嵌套类别

当单元测试失败时,是否有一个惯例会抛出许多类似的错误消息?

用两个字符串构建回文

mdates定位器在图表中显示不存在的时间间隔

有没有办法让Re.Sub报告它所做的每一次替换?

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

启动线程时,Python键盘模块冻结/不工作

如何在Python中创建仅包含完整天数的月份的列表

某些值的数值幂和**之间的差异

try 在单个WITH_COLUMNS_SEQ操作中链接表达式时,使用Polars数据帧时出现ComputeError

将标签与山脊线图对齐

Django-修改后的管理表单返回对象而不是文本

是否从Python调用SHGetKnownFolderPath?