Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.
In this problem, we will not be dealing with a minimum monthly payment rate.
The following variables contain values as described below:
balance
- the outstanding balance on the credit cardannualInterestRate
- annual interest rate as a decimalThe program should print out one line: the lowest monthly payment that will pay off all debt in under 1 year, for example:
Lowest Payment: 180
Assume that the interest is compounded monthly according to the balance at the end of the month (after the payment for that month is made). The monthly payment must be a multiple of $10 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme, which is okay. A summary of the required math is found below:
- Monthly interest rate = (Annual interest rate) / 12.0
- Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly payment)
- Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
这是我的密码。我不知道我错在哪里:
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))
我想这就是你要找的程序:
我留下了print语句,这样您就可以看到每次迭代中发生了什么。在
我在你的代码中发现了一些问题:
1)您正在执行一个
monthlyFixedPayment += 10
更改固定付款方式。你不应该根据你的问题定义改变固定付款。在2)您在外部
while
循环的每个迭代中执行checkBalance = balance
。这导致重置计算值。在3)我引入了一个count变量来检查这些破坏发生了多少次,因为month在每次迭代中都会被重置。在
while checkBalance <= 0:
到{另外,您需要在
while month <= 11:
循环中增加month
。在你的做法很困难;有一个固定付款的分析解决方案:
相关问题 更多 >
编程相关推荐