2016-02-13 115 views
0

我是Python的新手,正在嘗試計算並輸出基於4.5%年利率的貸款年度餘額。病程5年和原則= 1000,但我只能做到每年支付的金額,但資產負債不匹配這應該是要求:Python 3.5 - 每年貸款餘額

 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 

回答

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 
+0

TNK U,酷派非常簡單。我必須使用2個功能,1.計算每月支付和2.計算剩餘餘額。 MonthlyPayment = Principal * r(1 + r)n(1 + r)n-1 r是月利率(4.5/100)/ 12利率= 0 tuse:MonthlyPayment = Principal/n n = (5x12) 第二個函數計算並返回剩餘的貸款餘額。按照確切順序(本金,實際利率,期限,數量支付)接受4個四段。 本金:貸款總額。 – dprogram

+0

annual_interest_rate:4.5 number_of_payments:已支付的每月付款數量。 計算剩餘貸款餘額使用: 剩餘貸款餘額=本金*(1 + r)n-(1 + r)p(1 + r)n-1 r是月利率。 r的計算方法是首先將annual_interest_rate除以100,然後將結果除以12以使其成爲每月。如果利率= 0,則使用:RemainingLoanBalance =本金(1-pn) n是整個貸款期間的每月支付總額。 n =貸款年限x 12。p =支付金額。 Func應該返回剩餘的bal作爲float float – dprogram

+0

@dprogram,我在我的答案中添加了「**代碼(沒有numpy)**」部分 - 它不使用「numpy」模塊。 – MaxU