在房屋贷款中,还款是按月进行的,利息也是按月计算的(月利率为年利率除以12),每月还款优先还本月利息,剩下的还本金,未还款的本金会影响下月利息的计算,在还款期数达到时,所有贷款都会还完,还款选项中有等额本息和等额本金两种方式,等额本息每月的还款额是相同的,等额本金每月还款额中的本金的值是相同的,请编程计算两种还款方式的区别。
# coding=utf-8 # author:Darren # email:loccui@126.com #time:2020-07-02 import numpy as np import math # ********************第一种方法********************** # 房贷等额本息算法 # 计算房贷等额本息法应还款的额度并输出结果 def averageCapitalPlusInterest(principal, anualInterestRate, month): # principal表示贷款总额,anualInterestRate表示年利率,month表示房贷月份 monthlyPayment = np.around(principal * averageCapitalPlusInterestRate(month, monthlyInterestRate(anualInterestRate)), 2) # 计算每月应还款金额 totalPayment = np.around(monthlyPayment * month, 2) # 还款总额 print(monthlyPayment," ",monthlyPayment," ",totalPayment) return # 计算每月利率 def monthlyInterestRate(anualInterestRate): s = anualInterestRate / 12 return s # 计算比例系数 def averageCapitalPlusInterestRate(month, monthlyInterestRate): R = monthlyInterestRate N = month I = R * math.pow(1 + R, N) / (math.pow(1 + R, N) - 1) return I # 房贷等额本金算法 # 计算房贷等额本金法应还款的额度并输出结果 def averageCapital(principal, anualInterestRate, month): # principal表示房价总额,anualInterestRate表示房贷年利率,month表示房贷月份 monthlyPayment = np.zeros(month) # 初始化每月还款金额 for i in range(0, month): monthlyPayment[i] = np.around((principal / month) + ((principal - i * (principal / month)) * monthlyInterestRate(anualInterestRate)) , 2) # 每月还款金额 totalPayment = np.around(sum(monthlyPayment), 2) # 计算还款总额 print(monthlyPayment[1]," ",monthlyPayment[2]," ",totalPayment) return # 示例 ########### 主程序 ############## if __name__ == "__main__": t = float(input("t:")) r= float(input("r:")) m = int(input("m:")) print(t,' ',r,' ',m) averageCapitalPlusInterest(t, r, m) averageCapital(t, r, m) # ——————————————————第二种方法———————————————————————— # t = float(input("t:")) #贷款金额 # r= float(input("r:")) #年利率 # m = int(input("m:")) # 贷款时间月 # print(t,' ',r,' ',m) # s = r / 12 # 月利息 # #---------等额本息----------------- # I = s * math.pow(1 + s, m) / (math.pow(1 + s, m) - 1) # monthlyPayment1 = np.around(t * I,2) # 计算每月应还款金额 # totalPayment1 = np.around(monthlyPayment1 * m, 2) # 还款总额 # #--------- 等额本金----------------- # monthlyPayment2 = np.zeros(m) # 初始化每月还款金额 # for i in range(0, m): # monthlyPayment2[i] = np.around((t / m) + ((t - i * (t / m)) * s), 2) # 每月还款金额 # totalPayment2 = np.around(sum(monthlyPayment2), 2) # 计算还款总额 # print(monthlyPayment1,' ', monthlyPayment1,' ', totalPayment1) # print(monthlyPayment2[1],' ', monthlyPayment2[2],' ', totalPayment2)