1

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

它不必是一个表格,也许只是一个列表......在付款后列出付款,直到余额为 0 或 - 什么?

这是我到目前为止使用 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')

有任何想法吗?

4

1 回答 1

0

I would say the best thing for this is to use numpy! here is a general idea for the kind of code you would need:

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

now that you have an array of tables set up in a for loop (which I will not finish for you), you can use it in you function to iterate through and for every payment, set the cell to the correct value for the column. So if you calculate the interest amount in a variable interest, you just need to throw this in your for loop to make the interest that belongs in the right place to go there:

row[2] = str(interest)

interest is iterating at the same pace row is, so they should match up if done correctly

于 2013-04-17T21:38:44.930 回答