April 23rd, 2013, 10:23 AM

Python Code need to create table. HELP!
Hi I built this code that runs well. Is about calculating a student loan... The problem is that I wish I could create a table that will tell me in each column "payment number", "remaining balance", "interest amount", "principal amount" Can you help me do this? Im new to python and this is not for a python class... Im just trying to build a math model and I thought that since python is a free program and I have used it once before... that I could use it again during my presentation in class.
Edited!
IT DOESN'T HAVE TO BE AS A TABLE MAYBE JUST A LIST BREAKING DOWN ALL THE PAYMENTS TILL THE BALANCE IS 0 or SOMETHING... THANKS!
Does anyone know how I can accomplish this?
This is my code so far.
Code:
def calcDebt (principal, interestRate, numPayments, freqPayment):
#This code will make different predictions to pay off student loan
#Input Meanings
'''
Inputs
 interestRate  The Interest Rate of a Loan
 numPayments  The Number of Payments Needed
 principal  The Original Student Loan Amount
 freqPayment  Frequency of Payments Based on Weekly, Monthly, Annually
 m  The Minimum Payment Rate of Student Loan
Returns
 paymentAmount  The Payment Amount Will Be
 minimumPayment  The Least Amount Needed to Pay
'''
freqPayment_lookup = {'weekly': 52, 'monthly':12, 'annually':1}
interestRate = float(interestRate) / 100
x = interestRate/freqPayment_lookup[freqPaym…
y = (1.0 + x) ** numPayments
z = x/(y  1.0)
paymentAmount = (x + z) * principal
return paymentAmount
def main():
a = input('Student Loan Amount: ')
i = input('Student Loan Interest Rate: ')
n = input('Number of Payments: ')
f = None
while f not in ['weekly', 'monthly', 'annually']:
if f:
f = raw_input('Sorry! That is NOT an Option. Please Enter weekly, monthly, or annually: ').lower()
else:
f = raw_input('How Often Do You Want To Make Your Payments? ').lower()
m = input('Do You Know Your Minimum Payment Rate? If So, That is: ')
payment = calcDebt(a, i, n, f)
print 'Your %s payment will be %.2f' % (f, payment)
minimumPayment = a * m / 100
minToInterest = round((i/100)/12 *a)
minToPrincipal = round(minimumPayment  (i/100)/12 * a)
print 'You Must Pay ATLEAST the Minimum of:', minimumPayment
print 'IF Only Minimum is Paid, This Amount Goes to Interest:', minToInterest
print 'IF Only Minimum is Paid, This Amount Goes to Principal:', minToPrincipal
if __name__ == '__main__':
main()
raw_input('Please Press Enter to Exit')
Last edited by Juliayaho; April 25th, 2013 at 08:58 PM.
Reason: Added details to my question
April 23rd, 2013, 12:52 PM

Great idea!
Code:
...
Do You Know Your Minimum Payment Rate? If So, That is: no
Traceback (most recent call last):
File "q.py", line 53, in <module>
main()
File "q.py", line 42, in main
m = input('Do You Know Your Minimum Payment Rate? If So, That is: ')
File "<string>", line 1, in <module>
NameError: name 'no' is not defined
Not sure the program makes correct computations.
Code:
$ python q.py
Student Loan Amount: 12000
12000
Student Loan Interest Rate: 0.03
0.03
Number of Payments: 100
100
How Often Do You Want To Make Your Payments? monthly
monthly
Do You Know Your Minimum Payment Rate? If So, That is: 40
Your monthly payment will be 120.15
You Must Pay ATLEAST the Minimum of: 4800
IF Only Minimum is Paid, This Amount Goes to Interest: 0.0
IF Only Minimum is Paid, This Amount Goes to Principal: 4800.0
Please Press Enter to Exit
Do you want to run the program during your presentation? Just show a result table?
Comments on this post
Last edited by b49P23TIvg; April 23rd, 2013 at 12:57 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
April 23rd, 2013, 08:43 PM

Originally Posted by b49P23TIvg
Code:
...
Do You Know Your Minimum Payment Rate? If So, That is: no
Traceback (most recent call last):
File "q.py", line 53, in <module>
main()
File "q.py", line 42, in main
m = input('Do You Know Your Minimum Payment Rate? If So, That is: ')
File "<string>", line 1, in <module>
NameError: name 'no' is not defined
Not sure the program makes correct computations.
Code:
$ python q.py
Student Loan Amount: 12000
12000
Student Loan Interest Rate: 0.03
0.03
Number of Payments: 100
100
How Often Do You Want To Make Your Payments? monthly
monthly
Do You Know Your Minimum Payment Rate? If So, That is: 40
Your monthly payment will be 120.15
You Must Pay ATLEAST the Minimum of: 4800
IF Only Minimum is Paid, This Amount Goes to Interest: 0.0
IF Only Minimum is Paid, This Amount Goes to Principal: 4800.0
Please Press Enter to Exit
Do you want to run the program during your presentation? Just show a result table?
Well is because the interest rate should be in percent not decimal so instead of .03 u need to type 3 as in 3% also the minimum monthly payment rate is in percent so 40 means 40% too high which usually is about 2% what credit card or loaners ask for...
Is because I remember seeing another code on google that it displayed a table breaking down the payments that would be made until is paid off.. But I tried googleing it but I can't find it and I don't know how to display a table on python....
A friend gave me this info about it
Code:
import numpy as np
#this creates your table with numPayments rows and 4 columns
numPayments = 40
row_index = range(1,numPayments + 1)
real_row_index = []
for i in row_index:
real_row_index.append(str(i))
columns = ["payment number", "remaining balance", "interest amount", "principal amount"]
total_payments = np.chararray((numPayments,3), itemsize=20)
total_payments[:] = "none"
total_payments = np.insert(total_payments, 0, np.array((real_row_index)),1)
total_payments = np.insert(total_payments, 0, np.array((columns)),0)
for row in total_payments[1::,1::]:
#the 1 skips the lables so you don't mess with them
#row[0] will be the number payment column
#row[1] will be the remaining balance column
#row[2] will be the interest amount column
#row[3] will be the principal amount column
But I don't know what numpy is nor how to apply it to my code... Any ideas? Thanks for the reply
April 23rd, 2013, 09:16 PM

The enthought python distribution includes numpy, as does scipy www.scipy.org
Lists of lists (tables) don't print neatly in python. To get it right the algorithm needs to format each number, then line up (say) the decimal points, insert the right amount of white space for each field. Python lists hold arbitrary data; the cost of neatly formatting everything is too high for a task that doesn't even make sense. (A good reason python doesn't try. You might make headway inheriting from a pprint class.)
numpy arrays have (in the usual case) just a single data type. They display neatly. You don't need numpy for your application, just format each row with a format something like
Code:
'{:3d} ${:9.2f} ${:9.2f} ${:9.2f}'.format(n,remaining,interest,principal)
Last edited by b49P23TIvg; April 23rd, 2013 at 09:19 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
April 25th, 2013, 08:56 PM

Originally Posted by b49P23TIvg
The enthought python distribution includes numpy, as does scipy www.scipy.org
Lists of lists (tables) don't print neatly in python. To get it right the algorithm needs to format each number, then line up (say) the decimal points, insert the right amount of white space for each field. Python lists hold arbitrary data; the cost of neatly formatting everything is too high for a task that doesn't even make sense. (A good reason python doesn't try. You might make headway inheriting from a pprint class.)
numpy arrays have (in the usual case) just a single data type. They display neatly. You don't need numpy for your application, just format each row with a format something like
Code:
'{:3d} ${:9.2f} ${:9.2f} ${:9.2f}'.format(n,remaining,interest,principal)
What if I just want the program to List all the payments till the balance is 0 or something... ? Like what would be the code/formula that would tell me the payment then the interest/principal balance the again the next payment and so on? Thanks again
April 26th, 2013, 09:45 AM

ugly ugly
I think this is correct.
Code:
def getx(interestRate, freqPayment):
freqPayment_lookup = {'weekly': 52, 'monthly':12, 'annually':1}
return interestRate / (100.0*freqPayment_lookup[freqPayment])
def calcDebt(principal, interestRate, numPayments, freqPayment):
#This code will make different predictions to pay off student loan
#Input Meanings
'''
Inputs
 interestRate  The annual interest rate of a loan, percent
 numPayments  The Number of Payments Needed
 principal  The Original Student Loan Amount
 freqPayment  Frequency of Payments Based on Weekly, Monthly, Annually
Returns
 paymentAmount  The Payment Amount Will Be
 minimumPayment  The Least Amount Needed to Pay
'''
x = getx(interestRate, freqPayment)
y = (1.0 + x) ** numPayments
z = x/(y  1.0)
paymentAmount = (x + z) * principal
return paymentAmount
def main():
a = input('Student Loan Amount: ')
i = input('Student Loan Interest Rate (percent): ')
n = int(input('Number of Payments: '))
f = raw_input('How Often Do You Want To Make Your Payments? ').lower()
while f not in ['weekly', 'monthly', 'annually']:
f = raw_input('Sorry! That is NOT an Option. Please Enter weekly, monthly, or annually: ').lower()
payment = calcDebt(a, i, n, f)
print 'Your %s payment will be %.2f' % (f, payment)
total = payment * n
print("you'll pay a total of {:.2f}, of which {:.2f} is interest".format(total, total  a))
x = getx(i, f)
t = a
print('# toPrinc toIntst balance paid')
for k in range(n):
u = t
t *= 1 + x
t = payment
toPrincipal = u  t
toInterest = payment  toPrincipal
print(k + 1, toPrincipal, toInterest, t, (k + 1) * payment)
if __name__ == '__main__':
main()
raw_input('Please Press Enter to Exit')
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!
April 27th, 2013, 06:36 PM

Originally Posted by b49P23TIvg
I think this is correct.
Code:
def getx(interestRate, freqPayment):
freqPayment_lookup = {'weekly': 52, 'monthly':12, 'annually':1}
return interestRate / (100.0*freqPayment_lookup[freqPayment])
def calcDebt(principal, interestRate, numPayments, freqPayment):
#This code will make different predictions to pay off student loan
#Input Meanings
'''
Inputs
 interestRate  The annual interest rate of a loan, percent
 numPayments  The Number of Payments Needed
 principal  The Original Student Loan Amount
 freqPayment  Frequency of Payments Based on Weekly, Monthly, Annually
Returns
 paymentAmount  The Payment Amount Will Be
 minimumPayment  The Least Amount Needed to Pay
'''
x = getx(interestRate, freqPayment)
y = (1.0 + x) ** numPayments
z = x/(y  1.0)
paymentAmount = (x + z) * principal
return paymentAmount
def main():
a = input('Student Loan Amount: ')
i = input('Student Loan Interest Rate (percent): ')
n = int(input('Number of Payments: '))
f = raw_input('How Often Do You Want To Make Your Payments? ').lower()
while f not in ['weekly', 'monthly', 'annually']:
f = raw_input('Sorry! That is NOT an Option. Please Enter weekly, monthly, or annually: ').lower()
payment = calcDebt(a, i, n, f)
print 'Your %s payment will be %.2f' % (f, payment)
total = payment * n
print("you'll pay a total of {:.2f}, of which {:.2f} is interest".format(total, total  a))
x = getx(i, f)
t = a
print('# toPrinc toIntst balance paid')
for k in range(n):
u = t
t *= 1 + x
t = payment
toPrincipal = u  t
toInterest = payment  toPrincipal
print(k + 1, toPrincipal, toInterest, t, (k + 1) * payment)
if __name__ == '__main__':
main()
raw_input('Please Press Enter to Exit')
Thank you that works perfectly... i added a few lines that tell me what the minimum payment should be...
Code:
def getx(interestRate, freqPayment):
freqPayment_lookup = {'weekly': 52, 'monthly':12, 'annually':1}
return interestRate / (100.0*freqPayment_lookup[freqPayment])
def calcDebt(principal, interestRate, numPayments, freqPayment):
#This code will make different predictions to pay off student loan
#Input Meanings
'''
Inputs
 interestRate  The annual interest rate of a loan, percent
 numPayments  The Number of Payments Needed
 principal  The Original Student Loan Amount
 freqPayment  Frequency of Payments Based on Weekly, Monthly, Annually
Returns
 paymentAmount  The Payment Amount Will Be
 minimumPayment  The Least Amount Needed to Pay
'''
x = getx(interestRate, freqPayment)
y = (1.0 + x) ** numPayments
z = x/(y  1.0)
paymentAmount = (x + z) * principal
return paymentAmount
def main():
a = input('Student Loan Amount: ')
i = input('Student Loan Interest Rate (percent): ')
n = int(input('Number of Payments: '))
f = raw_input('How Often Do You Want To Make Your Payments? ').lower()
while f not in ['weekly', 'monthly', 'annually']:
f = raw_input('Sorry! That is NOT an Option. Please Enter weekly, monthly, or annually: ').lower()
m = input('What is Your Minimum Payment Rate (percent)? ')
payment = calcDebt(a, i, n, f)
print 'Your %s payment will be %.2f' % (f, payment)
minimumPayment = a * m / 100
minToInterest = round((i/100)/12 *a)
minToPrincipal = round(minimumPayment  (i/100)/12 * a)
print 'You Must Pay ATLEAST the Minimum of:', minimumPayment
print 'IF Only Minimum is Paid, This Amount Goes to Interest:', minToInterest
print 'IF Only Minimum is Paid, This Amount Goes to Principal:', minToPrincipal
total = payment * n
print("You'll Pay a Total of {:.2f}, Of Which {:.2f} is Interest".format(total, total  a))
x = getx(i, f)
t = a
print('#Pay toPrinc toIntst Balance Paid')
for k in range(n):
u = t
t *= 1 + x
t = payment
toPrincipal = u  t
toInterest = payment  toPrincipal
print(k + 1, toPrincipal, toInterest, t, (k + 1) * payment)
if __name__ == '__main__':
main()
raw_input('Please Press Enter to Exit')
by the way I was thinking since it can tell the minimum payment of my loan.. I was trying to find out the formula so that the program can tell me "well if you just pay the minimum payment it will take you this number of months to pay off your loan and youll end up paying this much in interest"
something like that like a total evaluation of what happens if only the minimum is given everytime... dont need a tabke for that just an overall of it... but i keep googleing for the formula (math major here not business major) and all i find is bak calculators that do tell me that but not the formula to get that....
anyways anyone know the formula or where I could find it?
thanks again!!!!! you are awesome b49P23TIvg!
April 27th, 2013, 07:49 PM

There are n variables, given n1 of them we can solve for the other. We'd need another equation to solve for 2 of the variables.
interest rate
compounding frequency (assumed to be same as payment frequency)
payment frequency
payment amount
number of payments (duration)
initial loan amount.
The loan is a scale factor, important because it converts to real money.
[code]
Code tags[/code] are essential for python code and Makefiles!
April 27th, 2013, 09:13 PM

Is it ok to say that to calculate the payoff date it be Loan*(interestRate)=interest then I add the interest to the loan and then divided by the minimum payment to get the months? Or am I way off?
Originally Posted by b49P23TIvg
There are n variables, given n1 of them we can solve for the other. We'd need another equation to solve for 2 of the variables.
interest rate
compounding frequency (assumed to be same as payment frequency)
payment frequency
payment amount
number of payments (duration)
initial loan amount.
The loan is a scale factor, important because it converts to real money.
April 28th, 2013, 08:07 AM

I don't really know anything about money except that you can't find any where I am.
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!