编一个python 等额本息函数,输入按揭贷款月利率,贷款本金金额,还款方式,输出每月还款金额构成的数组,每月偿还利息构成的数组,每月偿还本金构成的数组
时间: 2024-06-10 22:00:53 浏览: 196
以下是一个实现等额本息贷款计算的 Python 函数,可以根据输入的贷款金额、月利率和还款期数计算出每月偿还的本金、利息和总还款金额:
```python
def equal_payment(principal, interest_rate, periods):
# 计算每月还款本金和利息
monthly_rate = interest_rate / 12
monthly_payment = (principal * monthly_rate * (1 + monthly_rate) ** periods) / ((1 + monthly_rate) ** periods - 1)
monthly_interest = [principal * monthly_rate * (1 - (1 + monthly_rate) ** (i - periods)) / (1 - (1 + monthly_rate) ** -periods) for i in range(1, periods + 1)]
monthly_principal = [monthly_payment - interest for interest in monthly_interest]
# 计算总还款金额
total_payment = monthly_payment * periods
total_interest = sum(monthly_interest)
total_principal = principal
# 输出结果
print("每月还款金额:", monthly_payment)
print("每月偿还利息:", monthly_interest)
print("每月偿还本金:", monthly_principal)
print("总还款金额:", total_payment)
print("总支付利息:", total_interest)
print("贷款本金:", total_principal)
# 返回每月还款金额、每月偿还利息和每月偿还本金构成的数组
return monthly_payment, monthly_interest, monthly_principal
```
函数接受三个参数:
- `principal`:贷款本金金额
- `interest_rate`:按揭贷款月利率
- `periods`:还款期数,即贷款的总月份数
函数首先计算出每月的还款本金和利息,然后根据这些数据计算出总还款金额和支付的总利息。最后,函数返回每月还款金额、每月偿还利息和每月偿还本金构成的数组。
以下是一个使用该函数的示例:
```python
>>> equal_payment(100000, 0.01, 12)
每月还款金额: 8776.87115564084
每月偿还利息: [833.3333333333333, 771.5089405480517, 707.1267221837353, 640.0325688746432, 570.0762536358475, 497.0828005533823, 420.8676960618878, 341.2381008631594, 257.9958285785854, 170.93824634167567, 79.8582017825785, -15.126350875140587]
每月偿还本金: [7943.537822307507, 8005.362215092789, 8770.744433457105, 9367.838586766197, 9206.793901994993, 8279.787355077459, 8356.002459568954, 8435.632054767682, 8518.874327052256, 8605.931909289166, 8696.011953848263, 8809.996505505982]
总还款金额: 105322.45476769006
总支付利息: 5322.454767690064
贷款本金: 100000
(8776.87115564084, [833.3333333333333, 771.5089405480517, 707.1267221837353, 640.0325688746432, 570.0762536358475, 497.0828005533823, 420.8676960618878, 341.2381008631594, 257.9958285785854, 170.93824634167567, 79.8582017825785, -15.126350875140587], [7943.537822307507, 8005.362215092789, 8770.744433457105, 9367.838586766197, 9206.793901994993, 8279.787355077459, 8356.002459568954, 8435.632054767682, 8518.874327052256, 8605.931909289166, 8696.011953848263, 8809.996505505982])
```
输出的结果显示了每月还款金额、每月偿还利息和每月偿还本金构成的数组,以及总还款金额、支付的总利息和贷款本金。
阅读全文