George Frangs
George Frangs

Reputation: 23

Financial calculator Bond interest woes

One of my previous assignment submissions has been giving me a hard time. It's been a week of re-submitting and I'm not sure where I'm going wrong.

The feedback I was given says that under my "Bond" I have forgotten to turn the amount into a percentage by dividing it by 100. But I'm sure that's what I've done in line 35 i=((i/100)/12)/12.

These are the instructions from the textbook:

If the user selects ‘bond’, do the following:

The amount that a person will have to be repaid on a home loan each month is calculated as follows: repayment = x = (i.P)/(1 - (1+i)^(-n))

In the formulae above:

month and output the answer.

Here's my code, it's part of a bigger program.


    #Program that allows user access to two different financial calculators


import math


print('''Choose either 'Investment' or 'bond' from the menu below to proceed:

-----|Investment|-----  to calculate the amount of interest you'll earn in interest

-------|Bond|-------    to calculate the amount you'll have to pay on a home loan''')

#Input from user starts conditional statements

invest_or_bond = input(str("Please enter Investment or Bond\n" )).lower()

if invest_or_bond  == "investment":
    if True:
        p = float(input("How much are you depositing?\nR")) 
        r = float(input("At which interest rate percentile?\n" )) 
        r = (r/100) / 12
        t = float(input("How many years are you planning to invest for? \n"))  
        simp_comp = str(input("Choose 'Simple' or 'Compound' interest. \n")).lower() 
        
        if simp_comp == "simple":
            "simple" == simp_comp
            simp_comp = p*(1 + r * t) 
            total = simp_comp
            print (f"Your interest earned over {t} years will be R{interest_total:.2f}".format())
        elif simp_comp:
            simp_comp = p*math.pow((1+r),t) 
            total = simp_comp
            print (f"Your interest earned over {t} years will be R{interest_total:.2f}".format()) 

#Bond is another conditional
elif invest_or_bond == "bond":
    if True:
        p = float(input("What is the current value of the house?\nR")) 
        i = float(input("At which interest rate percentile?\n" )) 
        i = ((i/100)/12)/12 
        n = float(input("How many months you plan to repay? \n")) 
        monthly = float(math.floor((i*p)/(1 - (1+i)**(-n)))) 
        print(f"Your monthly repayment will be {monthly:.2f}".format())`  

else:
    print("Please enter a valid input. Try again.") 

I'm sure that I've answered the question, but I have been wrong before...

Any suggestions?

Upvotes: 0

Views: 856

Answers (2)

Evgeny
Evgeny

Reputation: 4561

As a suggestion for code structure improvement, you can also move some repeated code parts into own functions, separating your input/output code and financial calculations code. Here is a sample code:

def compound_interest(annual_rate, years):
    return (1 + annual_rate) ** years    

def simple_interest(annual_rate, years):
    return 1 + annual_rate * years    

def get_number(message):
    return float(input(message + "\n> "))
    
def get_investemnt_inputs():
    p = get_number("How much are you depositing?")
    r = get_number("At which annual interest rate (in percent)?")
    t = get_number("How many years are you planning to invest for?")
    return p, r, t
    
# input
p, r, t = get_investemnt_inputs()
while True:
    ans = input("[c]ompound or [f]lat rate?\n> ")
    if ans in ["c", "f"]:
        # select a proper interest rate function using a dictionary
        interest_func = dict(c=compound_interest, f=simple_interest)[ans]
        break
    else:
        print("Please enter c or f")
# all of the calculation logic here in one place
fv = p * interest_func(annual_rate=r / 100, years=t)
# output
currency = "R"
print(
    f"Future value of your initial investment of {currency}{p} "
    + f"over {t} years under {r}% interest rate will be {currency}{fv:.2f}"
)

Upvotes: 0

YoavHaPirjaj
YoavHaPirjaj

Reputation: 21

I think you might have divided your interest by 12 too much. Here's what I had on a project I was working on:

    print("Enter credit principal:")
    p = float(input())
    print("Enter count of periods:")
    n = float(input())
    print("Enter your credit interest:")
    i = float(input()) / 100 * (1/12)
    a = p * ((i * ((1 + i) ** n)) / (((1 + i) ** n) - 1))
    print("Your payment = {}!".format(math.ceil(a)))

Upvotes: 2

Related Questions