Search code examples
pythonclassobject-oriented-analysis

Creat a class called Investment with fields named principal and interest rate


I have the following problem. Write a class called Investment with fields called principal and interest. The constructor should set the values of those fields. There should be a method called value_after that returns the value of the investment after n years. The formula for this is p(1 + i)n, where p is the principal, and i is the interest rate.

I've already created a class and those asked methods.

class Investment:
    def __init__(self,principal,interest_rate):
        self.principal=principal
        self.interest_rate=interest_rate
    def value_after(self):
        n=int(input('Number of years\n'))
        return self.principal(1+self.interest_rate)**n    

final_result=Investment(float(input('Digit principal\n')),float(input('Digit interest rate\n')))
print('Final result is',final_result.value_after)

I expect to print the final result given in value_after function but when i run the program it gets the following warning: Final result is <bound method Investment.value_after of <__main__.Investment object at 0x7f0760110850>>


Solution

  • First of all, you have an indentation problem, the two line after def value_after(self): must be indented with respect to it: value_after has no body at all with your indentation.

    PS: I see you corrected it :-)