Home > Software engineering >  future value of monthly investments calculator with user input - Python
future value of monthly investments calculator with user input - Python

Time:11-03

I am trying to write a program that will calculate the future value of a monthly investment. Here is what I have so far:

def get_number(prompt, low, high):
    while True:
        number = float(input(prompt))
        if number > low and number <= high:
            is_valid = True
            return number
        else:
            print("Entry must be greater than", low,
                  "and less than or equal to", high,
                  "Please try again.")


def get_integer(prompt, low, high):
    while True:
        number = int(input(prompt))
        if number > low and number <= high:
            is_valid = True
            return number
        else:
            print("Entry must be greater than", low,
                  "and less than or equal to", high,
                  "Please try again.")


def calculate_future_value(monthly_investment, yearly_interest, years):
   # convert yearly values to monthly values
   monthly_interest_rate = ((yearly_interest / 100)   1) ** (1 / 12)
   months = years * 12

   # calculate future value
   future_value = 0.0
   for i in range(1, months):
       future_value  = monthly_investment
       monthly_interest = (future_value * monthly_interest_rate)-future_value
       future_value  = monthly_interest

   return future_value




def main():
    choice = "y"
    while choice.lower() == "y":
        # get input from the user
        monthly_investment = get_number("Enter monthly investment:\t", 0, 1000)
        yearly_interest_rate = get_number("Enter yearly interest rate:\t", 0, 15)
        years = get_integer("Enter number of years:\t\t", 0, 50)

        # get and display future value
        future_value = calculate_future_value(
            monthly_investment, yearly_interest_rate, years)

        print("Future value:\t\t\t"   str(round(future_value, 2)))

        # see if the user wants to continue
        choice = input("Continue? (y/n): ")

    print("Bye!")


if __name__ == "__main__":
    main()

Everything in the program is working fine except for the def calculate_future_value(monthly_investment, yearly_interest, years): section I believe I have a logic error but I can't find exactly what's going wrong.

The output should look like this Enter monthly investment: 350 Enter yearly interest rate: 10 Enter number of years: 36 Future value: 1484636.15 Continue? (y/n): n Bye!

But im getting Enter monthly investment: 350 Enter yearly interest rate: 10 Enter number of years: 36 Future value: 1312573.73 Continue? (y/n): no Bye!

CodePudding user response:

In testing out your code, I believe you really only need to correct one formula. The following statement does not appear to be correct.

monthly_interest_rate = ((yearly_interest / 100)   1) ** (1 / 12)

That would appear to raise your yearly interest rate to the "1/12th" power and not divide it into 1/12th of the yearly interest. I revised that formula as follows:

monthly_interest_rate = ((yearly_interest / 12 / 100)   1)

When I ran the program the value came out close to what you noted in your issue.

@Dev:~/Python_Programs/Investment$ python3 Invest.py 
Enter monthly investment:   350
Enter yearly interest rate: 10
Enter number of years:      36
Future value:           1472016.43
Continue? (y/n): n
Bye!

The difference between this value and the value you stated might be attributable to actually having the interest compounded daily each month.

But you might give this tweak a try and see if it meets the spirit of your project.

CodePudding user response:

It is possible to use numpy-financial to get the result.

import numpy_financial as npf

yrs = int(input('Enter number of years: '))
rate = float(input('Enter the annual rate of interest: '))
pmt = int(input('Enter the monthly payment: '))

future_amt = round(npf.fv(rate/12/100, yrs*12, -pmt, 0), 2)

print(future_amt)

This will run as:

Enter number of years: 36
Enter the annual rate of interest: 10
Enter the monthly payment: 350
1472366.43
  • Related