I am a new to python as a whole coming from COBOL.
I am trying to create a program to take user input about a loan, and then output a schedule of payments and how they will affect the balance of the loan.
#ACCEPTS USER INPUT AND ASSIGNS VALUES
print('lets try to amortize a loan')
input()
print('when will this loan begin?')
year = int(input('year: '))
month = int(input('month: '))
day = int(input('day: '))
principal = float(input('give me a loan amount: '))
rate = float(input('give me an interest rate in decimal form: '))
term = int(input('how many year will this loan be for: '))
comp = int(input('how many times a year will payments be made: '))
monthly = float(input('what will the payment amount be: '))
#DISPLAYS HOW THE SYSTEM TOOK USER INPUT
print('lets take a look at these values')
input()
print('\n', '$', principal,
'\n', (rate*100), '%',
'\n', term, 'years',
'\n', 'paying ', comp, 'times per year',
'\n', 'beginning:', month, '/', day, '/', year,
'\n', 'our monthly payment is $', monthly)
input()
#CALCULATES PERDIEM
perdiem = ((principal * rate) / 365)
print('our perdiem will be $', round(perdiem, 4))
print('with this we can calculate a months payment')
input()
print('first lets try and structure for the first month')
#BELOW IS TESTING TO SEE HOW MANY DAYS ARE IN A BILLING CYCLE
if month == 1 or 3 or 5 or 7 or 8 or 10 or 12:
NumDays = 31
elif month == 4 or 6 or 9 or 11:
NumDays = 30
elif month == 2 and (year % 4) != 0:
NumDays = 28
elif month == 2 and (year % 4) == 0:
NumDays = 29
print('so our first month would have', NumDays, 'days of perdiem')
input()
print('FIRST MONTH PAYMENT')
#BELOW TESTS TO SEE WHAT MONTH NEEDS TO BE DISPLAYED
if month == 12:
NextMonth = month - 11
NextYear = year + 1
else:
NextMonth = month + 1
NextYear = year
#DISPLAYS FIRST MONTHS DATA
print('Date:', NextMonth, '/', day, '/', NextYear)
PayNum = 1
print('Payment: #', PayNum)
print('Payment Amount: $', monthly)
print('Days this cycle:', NumDays)
MonthInt = round((perdiem * NumDays), 2)
print('Months Interest: $', MonthInt)
MonthP = round((monthly - MonthInt), 2)
print('Months Principal: $', MonthP)
balance = round((principal - MonthP), 2)
print('Remainder: $', balance)
input()
basically I would want the same information from the first months pay to be repeated with values being adjusted (paynum +1, nextmonth +1, balance - monthp, etc) until the balance is less than the monthly payment.
I have tried variations of if and while statements but cant seem to structure anything that works. My values will only adjust the one time and cant seem to continually overwrite values.
Help is appreciated on this issue as well as suggestions to better write my existing code.
My personal take is that input() function should be avoided at all costs when learning python: (a) it diverts you from structuring code in a modular way, (b) it is rarely used to applications - the interfaces are usually command line arguments or direct function calls.
The code you supply is a bit hard to follow, even though I appreciate it is linked to COBOL style and culture. The best thing to do is to devise your problem into smaller subtasks and test how they work in separation. This way you will know where your problem is, and it would be easier to focus your question (and attention of people answering).
My understanding is that the core idea of your script is calculating monthly payments and loan amortisation. Consider following code:
# FIXME: missing compound interest
def perdiem(amount, rate):
return (amount * rate) / 365
def days_per_month(month, year):
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month in [4, 6, 9, 11]:
return 30
elif month == 2:
if (year % 4) == 0:
return 29
else:
return 28
else:
raise ValueError('month expected', month)
def next_month(month, year):
if month == 12:
next_month = 1
next_year = year + 1
else:
next_month = month + 1
next_year = year
return next_month, next_year
def monthly_interest(principal, days, rate):
return perdiem(principal, rate) * days
def monthly_principal(monthly_payment, current_interest_payment):
return monthly_payment - current_interest_payment
def yield_expected_payment_schedule(start_year,
start_month,
initial_amount,
rate,
duration_in_years,
monthly_payment):
year = start_year
month = start_month
amount = initial_amount
for t in range(duration_in_years * 12 + 1):
days = days_per_month(month, year)
interest_due = monthly_interest(amount, days, rate)
if amount > 0:
yield {'balance at month start': amount,
'interest due': monthly_interest(amount, days, rate),
'year': year,
'month': month
}
amount = amount - (monthly_payment - interest_due)
month, year = next_month(month, year)
if __name__ == '__main__':
arg_dict1 = dict(start_year=1965,
start_month=8,
initial_amount=1000,
rate=0.05,
duration_in_years=1,
monthly_payment=85.61)
schedule = list(yield_expected_payment_schedule(**arg_dict1))
print(schedule)
Some follow-up thoughts:
try separate your argument collection, calculation and result presentation as much as possible
when you specifiy amount and duration you just know the monthly payment as an annuity, makes no sense to allow enter them both, consider devising evaluate_monthly_payment()
separate expected payments, amount paid and current amount due to make it more realistic
resort to datatime.date type and corresponding functions for date manipulation.
Hope it's useful.
"""
Hare is the Code for Amortization Schedule Calculator in Python
"""
def MortgageAmortizationSchedule(loan, months, interest):
monthlyPay = loan * (interest * (1 + interest) ** months / ((1 + interest) ** months - 1))
#print(-round(numpy.pmt(interest, months, loan), 2))
payment = monthlyPay
month = 1
monthlyInterest = interest * loan
principal = payment - monthlyInterest
balance = loan - principal
total_interest_paid = 0
print("#" * 105)
print("{0:>15}{1:>20} {2:>20} {3:>20} {4:>20}".format("Payment Month", "Principal Paid", "Interest Paid",
"Monthly Payment", "Mortgage Balance"))
print("#" * 105)
print("{0:>15}{1:>20,.2f} {2:>20,.2f} {3:>20,.2f} {4:>20,.2f}".format(month, principal, monthlyInterest,
payment, balance))
while balance > 0:
monthlyInterest = interest * balance
principal = payment - monthlyInterest
balance = balance - principal
month += 1
total_interest_paid += monthlyInterest
if balance <= 0:
balance = 0
print("{0:>15}{1:>20,.2f} {2:>20,.2f} {3:>20,.2f} {4:>20,.2f}".format(month, principal, monthlyInterest,
payment, balance))
print("#" * 105)
print("Total interest paid will be: ${0:,.2f}".format((payment * month) - loan))
print("Total principal paid will be: ${0:,.2f}".format(loan))
print("#" * 105)
############################## Inputs ##############################
loan = float(input('Enter Loan/Mortgage Amount: '))
years = float(input('Enter Loan Term/Period in years: '))
interest = float(input('Enter annual interest rate for this loan: '))
interest = interest / 12 / 100
months = years * 12
############## Create Amortization Schedule Calculator #############
MortgageAmortizationSchedule(loan, months, interest)
Related
*****UPDATE: Thanks to everyone who contributed. The code is alright. I figured there was a problem with the website I was using as IDLE.
I have to write a program in Python that calculates then prints specific information about a loan. The program (code below) consists of 2 functions and I have written them correctly. The only problem I have is that I have to write the second function within the first one. When I run the code, I get this error: NameError: name 'vaam' is not defined
I've just started coding last week, I hope you guys help me out with this.
Here's my program:
# Your function for calculating payment goes here
def loan(principal,annual_interest_rate,duration):
r=(annual_interest_rate)/1200
n=duration*12
if annual_interest_rate==0:
monthly_payment=principal/n
else:
monthly_payment=(principal*(r*(1+r)**n))/((1+r)**n-1)
return monthly_payment
# Your function for calculating remaining balance goes here
def vaam(principal, annual_interest_rate, duration , number_of_payments):
n=duration*12
r=(annual_interest_rate)/1200
if annual_interest_rate==0:
remaining_loan_balance=principal-principal*(number_of_payments/n)
else:
remaining_loan_balance=(principal*((1+r)**n-(1+r)**number_of_payments))/((1+r)**n-1)
return remaining_loan_balance
# Your main program goes here
principal=float(input("Enter loan amount: "))
annual_interest_rate=float(input("Enter annual interest rate (percent): "))
duration=int(input("Enter loan duration in years: "))
print('LOAN AMOUNT:',int(principal),'INTEREST RATE (PERCENT):',int(annual_interest_rate))
print('DURATION (YEARS):',int(duration),'MONTHLY PAYMENT:',int(loan(principal,annual_interest_rate,duration)))
for i in range(1,duration+1):
print('YEAR:',i,'BALANCE:',int(vaam(principal, annual_interest_rate, duration , i*12)),'TOTAL PAYMENT:',int(loan(principal,annual_interest_rate,duration)*12*i))
Try this:
# Your function for calculating payment goes here
def loan(principal, annual_interest_rate, duration):
r = (annual_interest_rate) / 1200
n = duration * 12
if annual_interest_rate == 0:
monthly_payment = principal / n
else:
monthly_payment = (principal * (r * (1 + r) ** n)) / ((1 + r) ** n - 1)
return monthly_payment
# Your function for calculating remaining balance goes here
def vaam(principal, annual_interest_rate, duration, number_of_payments):
n = duration * 12
r = (annual_interest_rate) / 1200
if annual_interest_rate == 0:
remaining_loan_balance = principal - principal * (number_of_payments / n)
else:
remaining_loan_balance = (principal * ((1 + r) ** n - (1 + r) ** number_of_payments)) / ((1 + r) ** n - 1)
return remaining_loan_balance
# Your main program goes here
principal = float(input("Enter loan amount: "))
annual_interest_rate = float(input("Enter annual interest rate (percent): "))
duration = int(input("Enter loan duration in years: "))
print('LOAN AMOUNT:', int(principal), 'INTEREST RATE (PERCENT):', int(annual_interest_rate))
print('DURATION (YEARS):', int(duration), 'MONTHLY PAYMENT:', int(loan(principal, annual_interest_rate, duration)))
for i in range(1, duration + 1):
print('YEAR:', i, 'BALANCE:', int(vaam(principal, annual_interest_rate, duration, i * 12)), 'TOTAL PAYMENT:',
int(loan(principal, annual_interest_rate, duration) * 12 * i))
Output
Enter loan amount: 5000
Enter annual interest rate (percent): 8
Enter loan duration in years: 5
LOAN AMOUNT: 5000 INTEREST RATE (PERCENT): 8
DURATION (YEARS): 5 MONTHLY PAYMENT: 101
YEAR: 1 BALANCE: 4152 TOTAL PAYMENT: 1216
YEAR: 2 BALANCE: 3235 TOTAL PAYMENT: 2433
YEAR: 3 BALANCE: 2241 TOTAL PAYMENT: 3649
YEAR: 4 BALANCE: 1165 TOTAL PAYMENT: 4866
YEAR: 5 BALANCE: 0 TOTAL PAYMENT: 6082
The issue is with your indentation, you put vaam function under loan function.
I'm trying to build a loan calculator that takes in the amount owed and interest rate per month and the down payment put at the beginning and the monthly installments so it can output how many months do you need to pay off your debt. The program works just fine when I enter an interest_rate below 10% but if I type any number for example 18% it just freezes and gives no output. When I stop running the program it gives me these errors:
Traceback (most recent call last):
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 35, in <module>
months_to_finish = get_months(price) # this returns the number of months from the counter var
File "C:\Users\Khaled\PycharmProjects\interest_payment\main.py", line 6, in get_months
price = price - installments
KeyboardInterrupt
This is my code:
def get_months(price):
counter = 0
price = price - down_payment
while price > 0:
price = price + price * interest_rate
price = price - installments
counter += 1
return counter
if __name__ == '__main__':
price = float(input("Enter price here: "))
interest_rate = float(input("Enter interest rate here: %")) / 100
while interest_rate < 0:
interest_rate = float(input('Invalid interest rate please try again: %')) / 100
down_payment = int(input("Enter your down payment here: $"))
while down_payment > price or down_payment < 0:
down_payment = int(input('Invalid down payment please try again: $'))
choice = input("Decision based on Installments (i) or Months to finish (m), please write i or m: ").lower()
if choice == 'm':
print('m')
elif choice == 'i':
installments = int(input("What's your monthly installment budget: ")) # get the installments
months_to_finish = get_months(price) # this returns the number of months from the counter var
print(f"It will take {months_to_finish} months to finish your purchase.")
else:
print('Invalid choice program ended')
These are the test values:
Enter price here: 22500
Enter interest rate here: %18
Enter your down payment here: $0
Decision based on Installments (i) or Months to finish (m), please write i or m: i
What's your monthly installment budget: 3000
With an initial principal of $22,500, an interest rate of 18%, a down payment of $0, and a monthly payment of $3,000, it will take an infinite number of months to pay off the loan. After one period at 18% interest, you have accrued $4,050 of interest. Since you're only paying $3,000 per period, you're not even covering the amount of new interest, and the total amount you owe will grow forever. You probably want to check somewhere that the monthly payment is greater than the first month's interest. You could modify your code like this:
if installments < (price - down_payment) * interest_rate:
print("The purchase cannot be made with these amounts.")
else:
months_to_finish = get_months(price)
print(f"It will take {months_to_finish} months to finish your purchase.")
l=[]
n=int(input())
i=0
for i in range(n):
l.append(str(input()))
def long(x):
if x>10:
return print(l[i][0]+str(len(l[i])-2)+l[i][-1])
def short(x):
if x<=10:
return print(l[i])
i=0
for i in range(len(l[i])):
long(len(l[i]))
short(len(l[i]))
continue
Python Learner. Working on a recurring monthly deposit, interest problem. Except I am being asked to build in a raise after every 6th month in this hypothetical. I am reaching the goal amount in fewer months than I'm supposed to.
Currently using the % function along with += function
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment= total_cost*.25
savings = 0
for i in range(300):
savings = monthly_savings*(((1+.04/12)**i) - 1)/(.04/12)
if float(savings) >= down_payment:
break
if i % 6 == 0 :
monthly_salary += monthly_salary * .03
monthly_savings = monthly_salary * portion_saved
Thanks for the advice all. My code is getting clearer and I reached correct outputs! The problem was with how and when I was calculating interest. In the case of a static contribution I successfully used the formula for interest on a recurring deposit, here, the simpler move of calculating interest at each month was needed to work with the flow of the loop.
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment = total_cost*.25
savings = 0
month = 1
while savings < down_payment :
print(savings)
savings += monthly_savings
savings = savings * (1+(.04/12))
month += 1
if month % 6 == 0 :
monthly_salary += (monthly_salary * semi_annual_raise)
monthly_savings = (monthly_salary * portion_saved)
print("")
print("it will take " + str(month) + " months to meet your savings goal.")
Does something like this work for you? Typically, we want to use while loops over for loops when we don't know how many iterations the loop will ultimately need.
monthly_savings = 1.1 # saving 10% each month
monthly_salary = 5000
down_payment = 2500
interest = .02
savings = 0
months = 0
while savings < goal:
print(savings)
savings = (monthly_salary * monthly_savings) + (savings * interest)
months += 1
if months % 6 == 0 :
monthly_salary += monthly_salary * .03
print("Took " + str(months) + " to save enough")
My assignment is to calculate how much money a person would get if his salary started at 1 cent per day and doubled every day.
days = int(input("How many days will you work for pennies a day?"))
total_amount = ((2 ** (days - 1)) / 100)
print("Days Worked | Amount Earned That Day")
for num in range(days):
total_amount = format((2 ** (num) / 100), ',.2f')
print(num + 1, "|", "$", total_amount)
If I enter 15 for days, I can see the salary on each day, but I need the total amount earned over the 15 days.
I need the total amount earned over the 15 days
As a standard for loop example you want summation over each iteration. To achieve this, you initialize variable (total_accumulated in this case) with 0 and then add to this variable each intermediate result from each iteration, after loop is complete you print out final accumulated result like so (minimal editing of your original code):
days = int(input("How many days will you work for pennies a day?"))
total_amount = ((2 ** (days - 1)) / 100)
total_accumulated = 0
print("Days Worked | Amount Earned That Day")
for num in range(days):
current_pay = (2 ** (num) / 100)
total_accumulated += current_pay
total_amount = format(current_pay, ',.2f')
print(num + 1, "|", "$", total_amount)
print("Total accumulated:", str(total_accumulated))
As noted in comment to your question by #NiVeR this can be calculated directly, and this answer is aimed only at example with loops since this looks like classic case of exercise.
Keep track of today salary and previous day salary. previous to calculate today salary and today salary to calculate total
init_sal = .01
total = 0
today_sal = 0
days = int(input("How many days will you work for pennies a day?"))
for x in range(1, days+1):
if x == 1:
today_sal = init_sal
prev_sal = today_sal
else:
today_sal = prev_sal * 2
prev_sal = today_sal
total += today_sal
print ('$', today_sal)
print (total)
Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.
In this problem, we will not be dealing with a minimum monthly payment rate.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
The program should print out one line: the lowest monthly payment that will pay off all debt in under 1 year, for example:
Lowest Payment: 180
Assume that the interest is compounded monthly according to the balance at the end of the month (after 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, which is okay. A summary of the required math is found below:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
Here is my code. I do not know where I'm going wrong:
balance = float(raw_input('enter the outsanding balance on your card'))
annualInterestRate = float(raw_input('enter the anual interest rate as a decimal'))
month = 0
checkBalance = balance
monthlyFixedPayment = 0
while checkBalance <= 0:
checkBalance = balance
monthlyFixedPayment += 10
while month <= 11:
monthlyInterestRate = annualInterestRate/12.0
monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
checkBalance = monthlyUnpaidBalance + (monthlyInterestRate * monthlyUnpaidBalance)
print('lowest payment:' + str(monthlyFixedPayment))
I think this is the program you are looking for:
balance = 500
annualInterestRate = .5
checkBalance = balance
monthlyFixedPayment = 10
count = 0
while checkBalance > 0:
month = 0
while month <= 11 and checkBalance > 0:
count+=1
monthlyInterestRate = annualInterestRate/12.0
monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
checkBalance = monthlyUnpaidBalance - (monthlyInterestRate * monthlyUnpaidBalance)
print "\t"+str(checkBalance)
month+=1
print checkBalance
print "lowest amount: "
print count*monthlyFixedPayment+checkBalance
I have left the print statements, so that you can see what is going on in each iteration.
Some problems i noticed in your code:
1) you were doing a monthlyFixedPayment += 10 that was changing the fixed payemnt. you are not supposed to change the fixed payment according to your problem definition.
2) you were doing a checkBalance = balance in each iteration of outer while loop. This was causing the calculated value to be resetted.
3) I have introduced a count variable to check how many times these decuctions were happening, as month was getting reset in each iteration.
while checkBalance <= 0: to while checkBalance >= 0:
Also, you need to increment month in the while month <= 11: loop.
You are going at it the hard way; there is an analytic solution for fixed_payment:
from math import ceil
def find_fixed_monthly_payment(balance, months, yearly_rate):
i = 1. + yearly_rate / 12.
im = i ** months
return balance * (im * (1. - i)) / (i * (1. - im))
def find_final_balance(balance, months, yearly_rate, fixed_payment):
i = 1. + yearly_rate / 12.
for _ in range(months):
# make payment
balance -= fixed_payment
# add interest
balance *= i
return balance
def get_float(prompt):
while True:
try:
return float(raw_input(prompt))
except ValueError:
# input could not be cast to float; try again
pass
def main():
balance = get_float("Please enter starting balance: ")
annual_rate = get_float("Annual interest rate (in percent): ") / 100.
fixed_payment = find_fixed_monthly_payment(balance, 12, annual_rate)
# round up to the nearest $10
fixed_payment = ceil(fixed_payment / 10.) * 10.
# double-check value of fixed_payment:
assert find_final_balance(balance, 12, annual_rate, fixed_payment ) <= 0., "fixed_payment is too low!"
assert find_final_balance(balance, 12, annual_rate, fixed_payment - 10.) > 0., "fixed_payment is too high!"
print("Lowest payment: ${:0.2f}".format(fixed_payment))
if __name__ == "__main__":
main()