Python 3.5 - 贷款年度余额

时间:2016-02-13 06:58:52

标签: python python-3.x

我是Python的新手,我正在尝试根据4.5%的年利率计算并输出贷款的年度余额。 5年的持续时间和原则= 1000但我只能完成每年支付的金额,但余额不符合应有的要求:

       balance total payment
year 1 817      223
year 2 626      447
year 3 427      671
year 4 218      894
year 5 0        1118

我的节目是:

def monthlyPayments(principle,annual_interest_rate,duration):
    global r, n

    r=(annual_interest_rate/100)/12 #monthly interest rate
    n=duration*pm#Total number of monthly payments for duration of loan
    f1=pow(1+r,n)
    f2=r*f1
    f3=pow(1+r,n)
    f4=f3-1
    f5=f2/f4
    monthlyPayment=principle*f5

    return monthlyPayment

def balance(principle,annual_interest_rate,duration,pm):
    f1=pow(1+r,n)
    f2=pow(1+r,pm)
    f3=f1-1
    f4=f1-f2
    f5=f4/f3
    remainingLoanBalance=principle*f5

    return remainingLoanBalance

principle=1000.0
annual_interest_rate=4.5
duration=5
pm=12#payments made
monthlyPayments=monthlyPayments(principle,annual_interest_rate,duration)
totalPayment=monthlyPayments*pm
balance =balance(principle,annual_interest_rate,duration,pm)
print("LOAN AMOUNT:",principle,"INTEREST RATE   (PERCENT):",annual_interest_rate, "\nDURATION (YEARS):", duration, "MONTHLY PAYMENT:", int(monthlyPayments))

for i in range(1,duration+1):
    print("YEAR:" ,i, "BALANCE:", int(balance), "TOTAL PAYMENT", int(totalPayment))

    totalPayment=totalPayment+monthlyPayments*pm
    balance=balance-monthlyPayments*pm

1 个答案:

答案 0 :(得分:0)

试试numpy,你会爱上它。 ;)

np.pmt() - 计算每月付款

np.fv() - 计算未来值

代码(使用 numpy ):

from __future__ import print_function
import numpy as np

amount = 1000
int_rate = 4.5/100
term = 5
nper = 12
mtly_pmt = -np.pmt(int_rate/nper, term*nper, amount)

fmt = 'year {0:2d} {1:8.2f} {2:8.2f}'

for y in range(1, term + 1):
    print(fmt.format(
            y,
            np.fv(int_rate/nper, y*nper, mtly_pmt, -amount),
            mtly_pmt * y * nper
          )
    )

输出:

year  1   817.55   223.72
year  2   626.72   447.43
year  3   427.12   671.15
year  4   218.36   894.86
year  5    -0.00  1118.58

代码(没有numpy):

from __future__ import print_function

# compute monthly payment
def pmt(rate, nper, pv):
    """
    rate    - annual interest rate
    nper    - # of periodic payments
    pv      - present value (principal loan amount)
    """
    return (rate * pv) / (1 - (1 + rate)**(-nper))

# compute future value (remaining balance)
def fv(rate, nper, pmt, pv):
    """
    rate    - annual interest rate
    nper    - # of periodic payments
    pmt     - monthly payment
    pv      - present value (principal loan amount)

    r1 = (1 + interest_rate) - will be used a few times below
    """
    r1 = 1 + rate
    return pv * r1**nper - pmt * (r1**nper -1) / rate


amount = 1000
int_rate = 4.5/100
term = 5
nper = 12
mtly_pmt = round(pmt(int_rate/nper, term*nper, amount), 2)

fmt = 'year {0:2d} {1:8.2f} {2:8.2f}'

for y in range(1, term + 1):
    print(fmt.format(
            y,
            fv(int_rate/nper, y*nper, mtly_pmt, amount),
            mtly_pmt * y * nper,
          )
    )

输出:

year  1   817.59   223.68
year  2   626.80   447.36
year  3   427.24   671.04
year  4   218.52   894.72
year  5     0.20  1118.40