I wanted the result to show the total amount of money after adding interest every year but it only increases the year but not the amount. Why?
while True:
try:
investment = float(input('How much to invest : '))
interest = float(input('Interest rate : '))
break
except ValueError:
"Please enter a valid number"
for year in range(10):
money = investment + (investment * interest)
print("Total money in year {} : {}".format((year+1), money))
It sounds like you need to accrue the interest:
for year in range(10):
investment += (investment * interest)
print("Total money in year {} : {}".format((year + 1), investment))
Logical error. Your investment variable does not be assigned each round in the loop.
Related
My program is supposed to tell users how many months it will take to double the money in their investment account. I am able to do the calculations correctly, but I'm unable to break out of the loop and print the statement that tells the user the final sentence "It will take x months to double your investment with a y% return".
balance = int(input("Enter an initial Roth IRA deposit amount:"))
apr = int(input("Enter an annual percent rate of return:"))
month = 0
while balance != 2*balance:
added_interest = balance * (apr / 100) / 12
balance = balance + added_interest
month +=1
formatted_balance = "${:.2f}".format(balance)
print("Value after month", month,":", formatted_balance)
if balance == 2*balance:
break
print("It will take", month, "months to double your investment with a", apr, "% return")
Your problem is that testing balance against 2*balance is always testing the current balance, not double the initial balance. Just store off the computed doubled balance initially, and test if the current balance is still less than that (no need for separate if/break, your while condition will handle it):
balance = int(input("Enter an initial Roth IRA deposit amount:"))
apr = int(input("Enter an annual percent rate of return:"))
month = 0
doubled_balance = 2 * balance # Store off doubled initial balance
while balance < doubled_balance: # Check current balance against doubled initial,
# and use <, not !=, so you stop when you exceed it,
# not just when you're exactly equal
added_interest = balance * (apr / 100) / 12
balance = balance + added_interest
month +=1
formatted_balance = "${:.2f}".format(balance)
print("Value after month", month,":", formatted_balance)
# No need for if/break
print("It will take", month, "months to double your investment with a", apr, "% return")
All that said, this doesn't need a loop at all. The initial balance doesn't matter (it takes just as long to double $1 as to double $1000 with a fixed rate of return, ignoring rounding errors), so this reduces to a simple conversion for APR to APY to account for monthly compounding, followed by a logarithm computation to figure out what power of the APY is necessary to reach 2 (a doubling), then convert from months to years and round up (since you won't double until the end of that month):
import math
apr = int(input("Enter an annual percent rate of return:"))
apr_decimal = apr / 100
apy = (1 + (apr_decimal / 12)) ** 12 # Standard APR to APY computation for monthly compounding
months = math.ceil(math.log(2, apy) * 12) # Compute the power (years) of APY to double the investment
# then multiply by 12 and round up to a full month
print("It will take", months, "months to double your investment with a", apr, "% return")
In your comparison you compared balance with 2*balance. Obviously balance == 2*balance will always be false, if balance > 0. So your code is stuck there forever if someone plans to invest anything more than $0.
You need a new variable to store the updated balance with rate of return:
# assuming you have balance and apr set
newBalance = balance # balance + returned amount
month = 0
while newBalance < 2*balance:
# replace ! with <, so that loop breaks when newBalance >= 2*balance
added_interest = newBalance * (apr / 100) / 12
newBalance = newBalance + added_interest
month +=1
formatted_balance = "${:.2f}".format(newBalance)
print("Value after month", month,":", formatted_balance)
The code above assumes that the added interest is calculated based on newBalance value. Please tell me if this does not work for you. I will debug it later.
Perhaps a little bit simpler solution.
from math import log, ceil
apr = int(input("Enter an annual percent rate of return: "))
rate = apr/12/100 # monthly rate
months = ceil(log(2)/rate)
print("It will take", months, "months to double your investment with a", apr, "% return")
prints:
Enter an annual percent rate of return: 10
It will take 84 months to double your investment with a 10 % return
This was calculated using the formula for continuous compounding. With a FV (future value) = PV (present value) * e**(rate * periods).
FV = PV * e**(rate*periods)
FV/PV = e**(rate*periods)
log(FV/PV) = log(e**(rate*periods))
log(FV/PV) = (rate*periods) * log(e)
log(FV/PV) = (rate*periods) * 1
log(FV/PV)/rate = periods
Since you want to find the months to double your initial investment, FV/PV will equal 2, so:
log(2)/rate = periods. (The log(e) == 1 above)
e and compound interest is defined here.
I have been working on this simple interest calculator and I was trying to make the for loop iterate until the amount inputted by the user is reached. But I am stuck at the range part, if I assign a range value like range(1 ,11) it will iterate it correctly and print the year in in contrast to the amount but I want the program to iterate until the year in which principal is greater than the amount is reached. My current code is bellow and the final product I want to reach is also attached bellow the current code. I'm new to python so please bare with me if I'm of track. Thanks in advance.
Current code:
principal = float(input("How much money to start? :"))
apr = float(input("What is the apr? :"))
amount = float(input("What is the amount you want to get to? :"))
def interestCalculator():
global principal
year = 1
for i in range(1, year + 1):
if principal < amount:
principal = principal + principal*apr
print("After year " + str (i)+" the account is at " + str(principal))
if principal > amount:
print("It would take" + str(year) + " years to reach your goal!")
else:
print("Can't calculate interest. Error: Amount is less than principal")
interestCalculator();
Final expected result:
Instead, you can use a while loop. What I mean here is you can simply:
principal = float(input("How much money to start? :"))
apr = float(input("What is the apr? :"))
amount = float(input("What is the amount you want to get to? :"))
def interestCalculator():
global principal
i = 1
if principal > amount:
print("Can't calculate interest. Error: Amount is less than principal")
while principal < amount:
principal = principal + principal*apr
print("After year " + str (i)+" the account is at " + str(principal))
if principal > amount:
print("It would take" + str(year) + " years to reach your goal!")
i += 1
interestCalculator()
A suggestion for a more pythonic solution
PRINCIPAL = float(input("How much money to start? :"))
APR = float(input("What is the apr? :"))
AMOUNT = float(input("What is the amount you want to get to? :"))
def interestCalculator(principal, apr, amount):
year = 0
yield year, principal
while principal < amount:
year += 1
principal += principal*apr
yield year, principal
for year, amount in interestCalculator(PRINCIPAL, APR, AMOUNT):
print(f"After year {year} the account is at {amount:.2f}")
if year == 0:
print("Can't calculate interest. Error: Amount is less than principal")
print(f"It would take {year} years to reach your goal!")
I have a question where im asked to calculate interest rate of an account after asking user for:
P is the present value of the account.
i is the monthly interest rate.
t is the number of months.
The current code I have is this:
def get_value(p, i , t):
return p * (1 + i) ** t
def main():
p = float(input("Please enter the current amount of money in your account: "))
i = float(input("Please enter the monthly interest rate: "))
t = float(input("Please enter the number of months: "))
#the while loop would probably go here, but I just dont know how to do it#
future_total = get_value(p, i, t)
print ("\nAfter", t, "months, you will have $", format(future_total, ".2f"), "in your account.")
main()
But the output is only giving me the final amount after 10 months, how do I implement a loop in order to see how much money would be in the account since month 1?
I would first make a variable called month and set it equal to 1. Then I would use the while loop so that when the month is less than the inputted month, the present value will be updated based on the inputted information. This will print out the money in the account for each month and not just the final value.
def get_value(p, i , t):
month = 1
while month <= t:
p = p * (1 + i)
print(month, p)
month += 1
def main():
p = float(input("Please enter the current amount of money in your account: "))
i = float(input("Please enter the monthly interest rate: "))
t = float(input("Please enter the number of months: "))
print(get_value(p,i,t))
# future_total = get_value(p, i, t)
# print ("\nAfter", t, "months, you will have $", format(future_total, ".2f"), "in your account.")
# print(f'After {t} months, you will have ${future_total} in your account.')
main()
Suppose you have an investment plan where you invest a certain fixed amount at the beginning of every year. Compute the total value of the investment at the end of the last year. The inputs will be the amount to invest each year, the interest rate, and the number of years of the investment.
This program calculates the future value
of a constant yearly investment.
Enter the yearly investment: 200
Enter the annual interest rate: .06
Enter the number of years: 12
The value in 12 years is: 3576.427533818945
I've tried a few different things, like below, but it doesn't give me that 3576.42, it gives me only $400. Any ideas?
principal = eval(input("Enter the yearly investment: "))
apr = eval(input("Enter the annual interest rate: "))
years = eval(input("Enter the number of years: "))
for i in range(years):
principal = principal * (1+apr)
print("The value in 12 years is: ", principal)
If it's a yearly investment, you should add it every year:
yearly = float(input("Enter the yearly investment: "))
apr = float(input("Enter the annual interest rate: "))
years = int(input("Enter the number of years: "))
total = 0
for i in range(years):
total += yearly
total *= 1 + apr
print("The value in 12 years is: ", total)
With your inputs, this outputs
('The value in 12 years is: ', 3576.427533818945)
Update: Responding to your questions from the comments, to clarify what's going on:
1) You can use int() for yearly and get the same answer, which is fine if you always invest a whole number of currency. Using a float works just as well but also allows the amount to be 199.99, for example.
2) += and *= are convenient shorthand: total += yearly means total = total + yearly. It's a little easier to type, but more important, it more clearly expresses the meaning. I read it like this
for i in range(years): # For each year
total += yearly # Grow the total by adding the yearly investment to it
total *= 1 + apr # Grow the total by multiplying it by (1 + apr)
The longer form just isn't as clear:
for i in range(years): # For each year
total = total + yearly # Add total and yearly and assign that to total
total = total * (1 + apr) # Multiply total by (1 + apr) and assign that to total
It can be done analytically:
"""
pmt = investment per period
r = interest rate per period
n = number of periods
v0 = initial value
"""
fv = lambda pmt, r, n, v0=0: pmt * ((1.0+r)**n-1)/r + v0*(1+r)**n
fv(200, 0.09, 10, 2000)
Similarly, if you are trying to figure out the amount you need to invest so you get to a certain number, you can do:
pmt = lambda fv, r, n, v0=0: (fv - v0*(1+r)**n) * r/((1.0+r)**n-1)
pmt(1000000, 0.09, 20, 0)
As suggested in the comments, you shouldn't use eval() here. (More info on eval can be found in the Python Docs). -- Instead, change your code to use float() or int() where applicable, as shown below.
Also, your print() statement printed out the parenthesis and comma, which I expect you didn't want. I cleaned it up in the code below, but if what you wanted is what you had feel free to put it back.
principal = float(input("Enter the yearly investment: "))
apr = float(input("Enter the annual interest rate: "))
# Note that years has to be int() because of range()
years = int(input("Enter the number of years: "))
for i in range(years):
principal = principal * (1+apr)
print "The value in 12 years is: %f" % principal
I am a python/programming beginner. I was assigned a problem on MIT open courseware to:
Write a program that calculates the minimum fixed monthly payment in order to pay off a credit card balance within 12 months.
Take as raw_input() the following floating point numbers:
1) the outstanding balance on the credit card
2) the annual interest rate as a decimal
Print out the fixed minimum payment, number of months(at most 12 and possibly less than 12) it takes to pay off the debt, and the balance (likely to be a negative number).
Assume the interest is compounded monthly according to the balance at the start of the month(before the payment for that month is made). The monthly payment must be a multiple of $10 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme.
THE ANSWER IS:
balance = float(raw_input('Enter the outstanding balance on your credit card: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal: '))
minPay = 10
newBalance = balance
while balance > 0:
for month in range(1,13):
newBalance = newBalance*(1+(interest/12))-minPay
if newBalance <=0:
break
if newBalance <= 0:
balance = newBalance
else:
newBalance = balance
minPay = minPay+10
print 'RESULT'
print 'Monthly payment to pay off debt in 1 year: ' + str(minPay)
print 'Number of months needed: ' + str(month)
print 'Balance: ' + str(round(balance,2))
MY QUESTIONS:
1) Using 1200 as the raw input balance, and .18 as the interest rate. Could someone explain in words how you would arrive at minPay = 120, month = 11, and balance = - 10.05?
I am confused by the newBalance = newBalance* (1 +(interest/12)) - minPay.
Using 1200 as the balance would make newBalance = 1200 * (1 +(.18/12)) - 10 = 1218 - 10 = 1208.
2) Since newBalance is not <= 0 the program would then proceed to the else statement. What is happening with the newBalance = balance part of the else statement. Does that assign newBalance back to 1200(original balance input).
I am having some trouble understanding this problem. Any insight whatsoever would be appreciated.
I would strongly recommend going through each line of the code as a python interpreter would and then seeing, why the program does perform as expected.
If you are lazy for that, try to put a print statement within the loop itself to see what the values of each variable are at every step. This always helps me figure out, what the code is doing.
As for your questions,
Yes, New balance does have the value that you expect it to have
Yes, it does get reassigned to balance, in the else part. You might want to change this.
As, I do not understand what the code is actually trying to do, I cannot help you with what the correct approach is, but try to add print statements, and it should help. Good Luck!
What is happening with the newBalance = balance part of the else statement? Does that assign newBalance back to 1200(original balance input).
Yes that's it. Then the minPay is increased and the loop starts again.
As minPay increases at each iteration, newBalance will end being negative.
balance = 3329
annualInterestRate = 0.2
minimum_fixed_payment = 10
unpaid_balance = balance
MonthlyInterestRate = annualInterestRate / 12
month_count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
while balance > 0:
for month in month_count:
unpaid_balance = unpaid_balance - minimum_fixed_payment
interest = MonthlyInterestRate * unpaid_balance
unpaid_balance = unpaid_balance + interest
if unpaid_balance <= 0:
break
if unpaid_balance <= 0:
balance = unpaid_balance
else:
unpaid_balance = balance
minimum_fixed_payment = minimum_fixed_payment + 10
print "Lowest Payment: %s" % (minimum_fixed_payment)
The easiest way to answer this question would be to see it from this angle and that is, using the minimum fixed payment, we subtract the minimum fixed payment from the unpaid balance every month and check if the value left is lesser than 0. if it is not we increase the minimum fixed payment by 10 and repeat the process until we have a balance that is lesser than or equal to zero.
That is the modification I made to the code.