我开始学习编码,从Python开始。在我的Python课程中,我遇到了以下问题:Write a program to calculate the credit card balance after one year if
a person only pays the minimum monthly payment required by the credit
card company each month.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
monthlyPaymentRate - minimum monthly payment rate as a decimal
For each month, calculate statements on the monthly payment and
remaining balance, and print to screen something of the format:
Month: 1
Minimum monthly payment: 96.0
Remaining balance: 4784.0
(我把那部分搞定了)Finally, print out the total amount paid that year and the remaining balance at the end of the year in the format:
Total paid: 96.0
Remaining balance: 4784.0
(这是总付费的一部分,我经过许多小时的尝试和搜索无法解决)
所以我需要做的是:把每月最低付款额的所有结果加起来,得到所付的总额。在
这是我的代码:def creditPayment(balance, annuelInterestRate, monthlyPaymentRate):
for month in range(1, 13):
monthlyInterestRate = annuelInterestRate/ 12.0
minimumMonthlyPayment = monthlyPaymentRate * balance
monthlyUnpaidBalance = balance - minimumMonthlyPayment
balance = monthlyUnpaidBalance + (monthlyInterestRate * monthlyUnpaidBalance)
totalPaid = sum((minimumMonthlyPayment) for _ in range(0, 13))
print 'Month: ', month
print 'Minimum monthly payment: ', round(minimumMonthlyPayment, 2)
print 'Remaining balance: ', round(balance, 2)
print ' '
print 'Total paid: ', round(totalPaid, 2)
print 'Remaining balance: ', round(balance, 2)
print creditPayment(4213, 0.2, 0.04)
一切都很好,除了总支付额加起来只有最低月供第一值的12倍。我不能再好了。在