Skip to main content
 首页 » 编程设计

python之如何使用此公式计算购买的每月付款

2024年12月31日8bonelee

支付=(费率*(1+费率)^N)/(((1+费率)^N)-1))

有人告诉我“编写一个 Python 模块 (loan.py),为贷款金额、月利率和数字赋值 付款,使用上述公式,计算每月付款。”

例如:“如果贷款金额为 10000 美元,月利率为 0.01(或年利率 12%),则 付款次数为 36,每月付款应为 332.14 美元。验证您的程序是否有效 通过使用此数据作为输入来正确地进行操作。”

我已尝试使用以下代码,但有人告诉我我正在“将 float 除以 0”。

l=eval(input("What's the loan amount?")) 
l=l*1.0 
r=eval(input("What's the monthy interest rate?(as a fraction)")) 
r=r*1.0 
N=eval(input("What's the number of payments you need to make?"))  
N=N*1.0 
P=((r*(1+r)**N)/((1+r**N)-1))*l 
print("Your monthly payment amount would be: ",P) 

如果您能告诉我我做错了什么,我将不胜感激。

请您参考如下方法:

interest_rate=eval(input("What's the loan amount?")) 
loan_amount=interest_rate*1.0 
 
interest_rate=eval(input("What's the monthy interest rate?(as a fraction)")) 
interest_rate=interest_rate*1.0 
 
number_of_payments=eval(input("What's the number of payments you need to make?"))  
number_of_payments=number_of_payments*1.0 
 
monthly_payment=((interest_rate*(1+interest_rate)**number_of_payments)/(((1+interest_rate)**number_of_payments)-1)) 
 
print("Your monthly payment amount would be: ",monthly_payment*loan_amount) 

请始终尝试在您的代码中使用有意义的变量名...这将使您的编程生活更轻松。