Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
compound_interest_calculator/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (58 sloc)
2.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator: | |
def __init__(self): | |
self.question_number = 0 | |
self.total = 0 | |
def calculator(self): # applies formula to the values | |
self.rate = self.rate / 100 | |
self.monthly = self.monthly * 12 | |
for i in range(0, self.years): | |
if self.total == 0: | |
self.total = self.account | |
self.total = (self.total + self.monthly) * (1 + self.rate) | |
print("Total amount of money you would have in your account after " + str(self.years) + " years: " + str(round(self.total, 2))) | |
def error(self): | |
if self.question_number == 1: | |
if self.account < 0: | |
return True | |
else: | |
return False | |
if self.question_number == 2: | |
if self.monthly < 0: | |
return True | |
else: | |
return False | |
else: | |
if 0 < self.rate < 100: | |
return False | |
else: | |
return True | |
def asker(self): # stores values from user inputs | |
if self.question_number == 0: | |
print("How many years will you be saving for? ") | |
self.years = int(input("Enter years: ")) | |
self.question_number += 1 | |
if self.question_number == 1: | |
print("How much money is currently in your account?") | |
self.account = float(input("Enter current amount in account: ")) | |
if self.error(): | |
self.asker() | |
else: | |
self.question_number += 1 | |
if self.question_number == 2: | |
print("How much money are you planning on investing monthly? ") | |
self.monthly = int(input("Enter amount: ")) | |
if self.error(): | |
self.asker() | |
else: | |
self.question_number += 1 | |
if self.question_number == 3: | |
print("What's the yearly interest rate of this investment?") | |
self.rate = float(input('Enter interest rate in percentage: ')) | |
if self.error(): | |
self.asker() | |
else: | |
self.question_number += 1 | |
if self.question_number == 4: | |
self.calculator() | |
c = Calculator() | |
c.asker() | |