0

I am starting to learn coding and beginning with Python. In my Python course I have a problem that is the following:

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

(I've got that part alright)

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

(It is the part of total paid that I can't resolve after many hours of trying and searching)

So here is what I need to do: add up together all the results of minimum monthly payment to get the total that was paid.

Here is my code:

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)

Everything works fine except for the total paid that adds up 12 times only the first value of minimumMonthlyPayment. I can't make it better.

2 Answers 2

1

If I've correctly interpreted your question, I'm assuming that your intent is to increment totalPaid by the minimumMonthlyPayment for each month. If so:

totalPaid = 0.0
for month in range(1, 13):
    #
    # Other stuff you're doing in the loop
    #
    totalPaid += minimumMonthlyPayment

Each time through the loop, totalPaid gets incremented by the minimum payment computed for that particular month. This logic of course has to be changed/augmented if you intend to add cases in which anything other than the minimum payment is paid.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks sooo much it worked perfectly! I just learned something new!
@MaryseGauthier In that case, awesome, and welcome to SO. :)
1

In your for loop, you have:

totalPaid = sum((minimumMonthlyPayment) for _ in  range(0, 13))

This is setting totalPaid to be 13 times the minimumMonthlyPayment, as it is for that iteration of the loop, so on the last loop the value is set to 13 times the last minimum payment. You need to add one value to the totalPaid each iteration, so that the value is updated before it is added. Here is what I would change the code to:

def creditPayment(balance, annuelInterestRate, monthlyPaymentRate):
    totalPaid = 0
    for month in range(1, 13):

        monthlyInterestRate = annuelInterestRate/ 12.0
        minimumMonthlyPayment = monthlyPaymentRate * balance
        monthlyUnpaidBalance = balance - minimumMonthlyPayment
        balance = monthlyUnpaidBalance + (monthlyInterestRate * monthlyUnpaidBalance)

        totalPaid +=minimumMonthlyPayment

        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)
print creditPayment(4213, 0.2, 0.04)

Also, since you aren't using the actual value of your iterator in for _ in range(0, 13), it would be more readable to just use range(13). I suspect you may have meant to have it loop 12 times, since the rest of your program does.

1 Comment

Thanks for your help as well! Actually the reason I put range (1, 13) is that I need to print Month: 1 to Month: 12 so the for month in range(1, 13) seemed to work fine to do that plus the rest of the job in the function. At least this is how I understood it. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.