在Python上创建一个平衡表(已编辑)

时间:2013-04-17 20:06:09

标签: python

我有这个代码,根据不同的因素显示我需要向学生贷款支付的款项,但我想知道如何展示一张告诉我的表...如果我的程序说我将制作36支付153.02美元,以6.4%的利息支付5000的贷款,我如何显示一张表告诉我(支付1中的153这是剩余的余额,这大部分用于支付利息或本金支付,然后同样支付2这个新的余额...等等......)换句话说,一张表告诉我这36笔付款以及每次付款后我的余额如何减少。

它不必作为一张桌子可能只是一张清单...付款后的上市付款除了平衡是0还是-SOMETHING?

这是我到目前为止使用python 2.7.3

的代码
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
    Returns
    - paymentAmount - The Payment Amount Will Be
    '''

    freqPayment_lookup = {'weekly': 52, 'monthly':12, 'annually':1}
    interestRate = float(interestRate) / 100

    x = interestRate/freqPayment_lookup[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: ')
    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()
    payment = calcDebt(a, i, n, f)
    print 'Your %s payment will be %.2f' % (f, payment)
if __name__ == '__main__':
    main()
    raw_input('Please Press Enter to Exit')

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我想说最好的事情就是使用numpy!以下是您需要的代码类型的一般概念:

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

现在您已经在for循环中设置了一组表格(我不会为您完成),您可以在函数中使用它来迭代并为每次付款,将单元格设置为正确的值专栏。因此,如果您计算可变利息中的利息金额,您只需要在您的for循环中抛出这个以使得属于正确位置的利益去那里:

row[2] = str(interest)

兴趣是以相同的速度迭代,所以如果正确完成它们应匹配