因此,我希望创建一个代码,根据年利率,本金金额和每月付款率计算最低月付款和剩余余额。所需的输出是:
Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 164.48
Remaining balance: 4013.2
依此类推,直到第12个月。
我知道有一种方法可以在没有定义功能的情况下完成它,但是整个功能只是让我搞砸了,所以我想尝试一下。我目前的代码是 -
a=0
while a<=11:
def min_mth_pay(balance,monthlyPaymentRate):
x = balance * monthlyPaymentRate
return x
def balance(balance,min_mth_pay,annualInterestRate):
y=(balance - min_mth_pay)*((annualInterestRate/12)+1)
return y
a +=1
print('Month:' + str(a) + 'Minimum monthly payment:' + str(x) + 'Remaining balance:' + str('y'))
我甚至不确定我是否可以使用这种格式的功能?弹出错误的名称是&#39; x&#39;未定义。 Python中真的很新,显然会欣赏任何澄清! :)
答案 0 :(得分:1)
使用调用来解决定义功能令人困惑。您应该单独定义函数,然后在循环中调用它们。
def min_mth_pay(balance,monthlyPaymentRate):
x = balance * monthlyPaymentRate
return x
def balance(balance,min_mth_pay,annualInterestRate):
y=(balance - min_mth_pay)*((annualInterestRate/12)+1)
return y
a=0
while a<=11:
a +=1
x = min_mth_pay(balance,monthlyPaymentRate)
y = balance(balance,min_mth_pay,annualInterestRate)
print('Month:' + str(a) + 'Minimum monthly payment:' + str(x) + 'Remaining balance:' + str(y))
请注意,不清楚代码中的balance,monthlyPaymentRate,min_mth_pay和annualInterestRate来自何处。