Home > Blockchain >  How would I add multiple attributes together and store them in another attribute in Object Oriented
How would I add multiple attributes together and store them in another attribute in Object Oriented

Time:05-21

Below is 1/4th of a class I am working on with multiple methods, I am struggling trying to figure out how to add all my attributes together and return them to the attribute total investment. I understand it maybe something very minor I am missing, but after researching which included 5 hours of youtube videos, and reading all about Object Oriented Programming, I am not finding a resolution that helps with having multiple items needing to be added together. Is it not possible? Or maybe I am not researching the correct question. Please let me know if you have any feedback/suggestions! Thank you in advance.

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
        

The blank output:

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: 

CodePudding user response:

Replace

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

with

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

and you should be good to go. You don't necessarily have to return something at the end of your method.

CodePudding user response:

I don't think this code is doing what you want it to.

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

A couple of things, first is that from what I can tell, you are not printing the value anywhere. You are simply returning it. So unless you are printing the result from your call to Income(), then it will not be printed. Since nothing is showing up in your output, I'm going to guess that you are not. Secondly, I think you are trying to assign those additions to self.totalmonthlyincome. To do this:

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

The == operator is a comparison operator. It's not for assignments, and usually doesn't make sense to include in a math operation. An example usage:

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