现在编写一个程序,计算所需的最低固定月付款,以便在12个月内偿还信用卡余额。按固定的每月付款,我们指的是每个月不变的单个数字,而是每月支付的固定金额。
在这个问题上,我们不会处理最低月付款率。
以下变量包含如下所述的值:
balance
- 信用卡上的未结余额annualInterestRate
- 年度小费率该计划应打印出一行:每月支付的最低金额,以偿还1年内的所有债务,例如:
Lowest Payment: 180
假设根据月末的余额(在该月的付款之后)每月复利。每月付款必须是10美元的倍数,并且所有月份都相同。请注意,使用此付款方案可能会使余额变为负数,这是可以的。所需数学的摘要如下:
- 每月利率=(年利率)/ 12.0
- 每月未付余额=(以前的余额) - (每月最低固定付款额)
- 每月更新余额=(每月未付余额)+(每月利率x每月未付余额)
这是我的代码。我不知道我哪里出错:
balance = float(raw_input('enter the outsanding balance on your card'))
annualInterestRate = float(raw_input('enter the anual interest rate as a decimal'))
month = 0
checkBalance = balance
monthlyFixedPayment = 0
while checkBalance <= 0:
checkBalance = balance
monthlyFixedPayment += 10
while month <= 11:
monthlyInterestRate = annualInterestRate/12.0
monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
checkBalance = monthlyUnpaidBalance + (monthlyInterestRate * monthlyUnpaidBalance)
print('lowest payment:' + str(monthlyFixedPayment))
答案 0 :(得分:1)
我认为这是您正在寻找的计划:
balance = 500
annualInterestRate = .5
checkBalance = balance
monthlyFixedPayment = 10
count = 0
while checkBalance > 0:
month = 0
while month <= 11 and checkBalance > 0:
count+=1
monthlyInterestRate = annualInterestRate/12.0
monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
checkBalance = monthlyUnpaidBalance - (monthlyInterestRate * monthlyUnpaidBalance)
print "\t"+str(checkBalance)
month+=1
print checkBalance
print "lowest amount: "
print count*monthlyFixedPayment+checkBalance
我已经离开了print语句,因此您可以看到每次迭代中发生了什么。
我在您的代码中注意到的一些问题:
1)你正在做一个正在改变固定工资的monthlyFixedPayment += 10
。您不应该根据您的问题定义更改固定付款。
2)你在外部checkBalance = balance
循环的每次迭代中都进行了while
。这导致重置计算值。
3)我已经引入了一个count变量来检查这些decuctions发生了多少次,因为每个迭代都会重置月份。
答案 1 :(得分:0)
while checkBalance <= 0:
至while checkBalance >= 0:
此外,您需要在month
循环中增加while month <= 11:
。
答案 2 :(得分:0)
你正在艰难前行;有一个针对fixed_payment的分析解决方案:
from math import ceil
def find_fixed_monthly_payment(balance, months, yearly_rate):
i = 1. + yearly_rate / 12.
im = i ** months
return balance * (im * (1. - i)) / (i * (1. - im))
def find_final_balance(balance, months, yearly_rate, fixed_payment):
i = 1. + yearly_rate / 12.
for _ in range(months):
# make payment
balance -= fixed_payment
# add interest
balance *= i
return balance
def get_float(prompt):
while True:
try:
return float(raw_input(prompt))
except ValueError:
# input could not be cast to float; try again
pass
def main():
balance = get_float("Please enter starting balance: ")
annual_rate = get_float("Annual interest rate (in percent): ") / 100.
fixed_payment = find_fixed_monthly_payment(balance, 12, annual_rate)
# round up to the nearest $10
fixed_payment = ceil(fixed_payment / 10.) * 10.
# double-check value of fixed_payment:
assert find_final_balance(balance, 12, annual_rate, fixed_payment ) <= 0., "fixed_payment is too low!"
assert find_final_balance(balance, 12, annual_rate, fixed_payment - 10.) > 0., "fixed_payment is too high!"
print("Lowest payment: ${:0.2f}".format(fixed_payment))
if __name__ == "__main__":
main()