def bank(balance, extra, time, intrestRate):
intrest = (balance extra * time * intrestRate )/100
return intrest
balance = float(input("Enter current balance:"))
extra = float(input("Amount added at end of year:"))
time = int(input("How many years:"))
intrestRate = float(input("Amount of intrest:"))
intrest = bank(balance, extra, time, intrestRate)
for i in range(time):
amount = extra balance * (1.0 intrestRate) ** i
print("Amount:", amount)
So this is what I have, and I think the issue is whenever adding the extra amount at the end of each year something is wrong with how I a doing the math.. ( Not my strong suite )
Using https://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php
To check my calculations and it seems like the first year comes out correct but... everything else after that is wrong.
CodePudding user response:
Why do you need to use for i in range(time):
? Can't you just do amount = extra balance * (1.0 intrestRate) ^ time
? Also, you are only adding "extra" once, you should add it for every year.
CodePudding user response:
Compound Interest Formula:
A represents the final amount in the account
P is the principal (the original amount of money)
t represents the time in years
n represents the number of times per year, interest is compounded
r represents the rate of interest
Code:
n = int(input("Number of times per year, interest is compounded"))
.
.
.
amount = balance * (1.0 intrestRate/n) ** (i*n)
Example:
If you have a bank account whose principal = $1,000, and your bank compounds the interest twice a year at an interest rate of 5%, how much money do you have in your account at the year's end?
CodePudding user response:
I understand the problem. This should work for you. I have also included plotting since you mentioned that it was for an assignment. You can remove it if you don't need it.
Note: install matplotlib if not done already: (pip install matplotlib)
import matplotlib.pyplot as plt
principal = float(input("Enter current balance:"))
extra = float(input("Amount added at end of year:"))
years = int(input("How many years:"))
rate = float(input("Amount of intrest:"))/100
# initially, the amount is the principal
# extra is only added at the end of the year
amount = principal
# interest earned is also 0 initially
interest = 0
# needed for plotting
# Comment these out if you don't want to plot
y = [principal]
x = [i for i in range(years 1)]
print(f"year: 0\tamount:{amount}")
for year in range(1, years 1):
interest = amount * rate # calculate the interest on the current amount
amount = amount interest extra # add the extra and interest
y.append(amount) # comment this out if plotting not needed
print(f"year: {year}\tInterest: {round(interest,2)}\tAmount: {round(amount,2)}\n")
print(f"Total Interest Earned: {interest}, Amount: {amount}")
plt.plot(x,y, marker="o")
plt.xlabel("Amount")
plt.ylabel("Years")
plt.show()