在 Python 的面向对象编程中,如何将多个属性加在一起并将它们存储在另一个属性中?

How would I add multiple attributes together and store them in another attribute in Object Oriented Programming in Python?

下面是 class 的 1/4 我正在使用多种方法,我正在努力尝试弄清楚如何将我的所有属性加在一起并将它们 return 添加到属性总数投资。我知道这可能是我遗漏的一些非常小的东西,但是在研究了其中包括 5 小时的 youtube 视频并阅读了所有关于面向对象编程的内容之后,我没有找到有助于将多个项目添加在一起的解决方案。不可能吗?或者也许我没有研究正确的问题。如果您有 feedback/suggestions,请告诉我!提前谢谢你。

class CalculationofRentalIncome():
    """A four step method to find out the calculation of rental income. ROI"""
    def __init__(self):
        self.totalmonthlyincome = 0
        self.totalmonthlyexpenses = 0
        self.totalmonthlycashflow = 0
        self.totalinvestment = 0
        self.returnOnInvestment = 0

    def Income(self):
        """ Monly income for possible rental property"""
        self.rent = int(input("Please enter the estimated amount of rental income: "))
        self.laundry = int(input(f"Please enter the estimated laundry income: "))
        self.storage = int(input(f"Please enter the estimated storage income: "))
        self.miscellaneousincome = int(input("Please enter the estimated miscellaneous income: "))

        if self.rent >= 0:
            print(f"Your monthly estimated rent is {self.rent}")
        elif self.rent < 0:
            print("Invalid input, please try again.")

        if self.laundry >= 0:
            print(f"Your montly laundry income is {self.laundry} ")
        elif self.laundry < 0:
            print("Invalid Input, please try again.")

        if self.storage >= 0:
            print(f" Your monthly storage income is {self.storage}")
        elif self.storage < 0:
            print("Invalid Input, please try again.")

        if self.miscellaneousincome >= 0:
            print(f"Your monthly miscellaneous is {self.miscellaneousincome}")
        elif self.miscellaneousincome < 0:
            print("Invalid Input, please try again.")

        print(f"The total monthly income is: ")
        return self.rent + self.laundry + self.storage +  self.miscellaneousincome == 
self.totalmonthlyincome
        

空白输出:

Would you like the calculation of rental income ? [Y] yes, [N] no y
Please enter the information accordingly, if you don't, have a budget listed or the 
information available, input the number 0
Please enter the estimated amount of rental income: 2000
Please enter the estimated laundry income: 500
Please enter the estimated storage income: 50
Please enter the estimated miscellaneous income: 100
Your monthly estimated rent is 2000
Your monthly laundry income is 500 
Your monthly storage income is 50
Your monthly miscellaneous is 100
The total monthly income is: 

替换

return self.rent + self.laundry + self.storage +  self.miscellaneousincome == 
self.totalmonthlyincome

self.totalmonthlyincome = self.rent + self.laundry + self.storage +  self.miscellaneousincome
print(self.totalmonthlyincome)

你应该可以开始了。你不一定要 return 在你的方法结束时做一些事情。

print(f"The total monthly income is: ")
return self.rent + self.laundry + self.storage +  self.miscellaneousincome == self.totalmonthlyincome

有几件事,首先是据我所知,您没有在任何地方打印值。你只是在退货。因此,除非您打印调用 Income() 的结果,否则它不会被打印。由于您的输出中没有显示任何内容,我猜您没有。其次,我认为您正试图将这些添加分配给 self.totalmonthlyincome。为此:

self.totalmonthlyincome = self.rent + self.laundry + self.storage +  self.miscellaneousincome
print(f"The total monthly income is: {self.totalmonthlyincome}")

== 运算符是比较运算符。它不用于作业,通常没有意义包含在数学运算中。用法示例:

if self.totalmonthlyincome == 0:
   print("Things are looking bleak!")