python-3.x - Python3如何创建一个返回抵押场景模拟的函数
问题描述
我需要帮助来计算抵押贷款的总成本和给定抵押贷款期限内产生的总利息。基本上,在贷款期限结束时,期末余额将为零。
从输出中,我不确定如何计算所产生的总利息和抵押总成本(抵押金额+总利息)的总和。
此外,我希望第 1 年的起始金额为抵押金额(即示例中的 1,000,000),但输出显示为 (900,000)
最后,我需要帮助来创建一个计算这些代码的函数,以使测试用例更容易。请帮助我,谢谢!
mortgage_amount = float(input('Enter mortgage loan amount: '))
duration = int(input('Enter the number of period in years: '))
fixed_payment = float(input('Enter the fixed payment amount per period: '))
interest = float(input('Enter the annual interest rate: '))
frequency = int(input('Enter the number of times payment occur per year: '))
print('\n')
interest_converted = interest / 100 / frequency
interest_payment = interest_converted * beginning_balance
beginning_balance = mortgage_amount
for i in range(0, duration):
interest_payment = beginning_balance * interest_converted
interest_payment_TVOM = interest_payment * (1 + interest_converted)
ending_balance = beginning_balance - fixed_payment
beginning_balance = ending_balance
total_payment = interest_payment_TVOM + fixed_payment
print('Year:{:1}, Beginning Balance: ${:.0f}, Interest Payment: ${:.0f}, Fixed Payment: ${:.0f}, Total Payment: ${:.0f}, Ending Balance: ${:.0f}'.format(i + 1, beginning_balance, interest_payment_TVOM, fixed_payment, total_payment, ending_balance))
print('\n')
print('total payment: ', total_payment)
print('total interest: ', interest_payment)
输出:
Enter mortgage loan amount: 1000000
Enter the number of period in years: 10
Enter the fixed payment amount per period: 100000
Enter the annual interest rate: 1.5
Enter the number of times payment occur per year: 1
Year:1, Beginning Balance: $900000, Interest Payment: $15225, Fixed Payment: $100000, Total Payment: $115225, Ending Balance: $900000
Year:2, Beginning Balance: $800000, Interest Payment: $13702, Fixed Payment: $100000, Total Payment: $113702, Ending Balance: $800000
Year:3, Beginning Balance: $700000, Interest Payment: $12180, Fixed Payment: $100000, Total Payment: $112180, Ending Balance: $700000
Year:4, Beginning Balance: $600000, Interest Payment: $10657, Fixed Payment: $100000, Total Payment: $110658, Ending Balance: $600000
Year:5, Beginning Balance: $500000, Interest Payment: $9135, Fixed Payment: $100000, Total Payment: $109135, Ending Balance: $500000
Year:6, Beginning Balance: $400000, Interest Payment: $7612, Fixed Payment: $100000, Total Payment: $107612, Ending Balance: $400000
Year:7, Beginning Balance: $300000, Interest Payment: $6090, Fixed Payment: $100000, Total Payment: $106090, Ending Balance: $300000
Year:8, Beginning Balance: $200000, Interest Payment: $4568, Fixed Payment: $100000, Total Payment: $104568, Ending Balance: $200000
Year:9, Beginning Balance: $100000, Interest Payment: $3045, Fixed Payment: $100000, Total Payment: $103045, Ending Balance: $100000
Year:10, Beginning Balance: $0, Interest Payment: $1522, Fixed Payment: $100000, Total Payment: $101522, Ending Balance: $0
total payment: 101522.5
total interest: 1500.0
解决方案
所以你需要做一点关于“抵押数学”的背景知识。如果您搜索“摊销计算(或公式)”并查看如何根据(贷款金额、利率、期数)计算付款,则有很多示例。然后您可以计算固定付款。从那里,您可以再做一些工作,找出每个支付期间的利息和本金成分的公式,您可以在循环中计算这些公式。
上手之后。:) 您可以在循环之外设置一个累积变量,以跟踪本金的运行总额(最后应该加上贷款金额)和利息。
在伪代码中:
tot_payments = 0 # to gather the total principal payments
tot_interest = 0 # to gather the interest
for period in range(num_periods):
# calculate principal and interest
# add to the accumulations like...
tot_payment += payment
推荐阅读
- javascript - 使用 Jquery 突出显示活动菜单
- jquery - 来自未存储在数据库中的输入的表单数据
- reactjs - reactjs中如何通过get方法显示图片?
- c++ - 检查 C++ 输入是否为整数的现代方法
- node.js - Sequelize ORM 不能使用 sync()
- java - 如何从类加载器加载的 jar 中获取 System out 控制台?
- c# - 即使使用异常处理中间件也能中断异常
- ssl - Mosquitto over SSL 拒绝发布消息
- python - 在 C/C++ 中嵌入 Python
- python - 如何为仅在 Python >(3,0) 中支持的模块编写单元测试,而所有版本都支持其余模块?