简体   繁体   中英

Can someone explain this python while loop to me?

I am a python/programming beginner. I was assigned a problem on MIT open courseware to:

Write a program that calculates the minimum fixed monthly payment in order to pay off a credit card balance within 12 months.

Take as raw_input() the following floating point numbers:

1) the outstanding balance on the credit card 2) the annual interest rate as a decimal

Print out the fixed minimum payment, number of months(at most 12 and possibly less than 12) it takes to pay off the debt, and the balance (likely to be a negative number).

Assume the interest is compounded monthly according to the balance at the start of the month(before 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.

THE ANSWER IS:

balance = float(raw_input('Enter the outstanding balance on your credit card: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal: '))
minPay = 10
newBalance = balance

while balance > 0:
    for month in range(1,13):
        newBalance = newBalance*(1+(interest/12))-minPay
        if newBalance <=0:
            break
    if newBalance <= 0:
        balance = newBalance
    else:
        newBalance = balance
        minPay = minPay+10


print 'RESULT'
print 'Monthly payment to pay off debt in 1 year: ' + str(minPay)
print 'Number of months needed: ' + str(month)
print 'Balance: ' + str(round(balance,2))

MY QUESTIONS:

1) Using 1200 as the raw input balance, and .18 as the interest rate. Could someone explain in words how you would arrive at minPay = 120, month = 11, and balance = - 10.05?

I am confused by the newBalance = newBalance* (1 +(interest/12)) - minPay.

Using 1200 as the balance would make newBalance = 1200 * (1 +(.18/12)) - 10 = 1218 - 10 = 1208.

2) Since newBalance is not <= 0 the program would then proceed to the else statement. What is happening with the newBalance = balance part of the else statement. Does that assign newBalance back to 1200(original balance input).

I am having some trouble understanding this problem. Any insight whatsoever would be appreciated.

I would strongly recommend going through each line of the code as a python interpreter would and then seeing, why the program does perform as expected.

If you are lazy for that, try to put a print statement within the loop itself to see what the values of each variable are at every step. This always helps me figure out, what the code is doing.

As for your questions,

  1. Yes, New balance does have the value that you expect it to have

  2. Yes, it does get reassigned to balance, in the else part. You might want to change this.

As, I do not understand what the code is actually trying to do, I cannot help you with what the correct approach is, but try to add print statements, and it should help. Good Luck!

What is happening with the newBalance = balance part of the else statement? Does that assign newBalance back to 1200(original balance input).

Yes that's it. Then the minPay is increased and the loop starts again.

As minPay increases at each iteration, newBalance will end being negative.

balance = 3329
annualInterestRate = 0.2

minimum_fixed_payment = 10
unpaid_balance = balance
MonthlyInterestRate = annualInterestRate / 12
month_count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

while balance > 0:
    for month in month_count:
        unpaid_balance = unpaid_balance - minimum_fixed_payment
        interest = MonthlyInterestRate * unpaid_balance
        unpaid_balance = unpaid_balance + interest

        if unpaid_balance <= 0:
            break
    if unpaid_balance <= 0:
        balance = unpaid_balance
    else:
        unpaid_balance = balance
        minimum_fixed_payment = minimum_fixed_payment + 10


print "Lowest Payment: %s" % (minimum_fixed_payment)

The easiest way to answer this question would be to see it from this angle and that is, using the minimum fixed payment, we subtract the minimum fixed payment from the unpaid balance every month and check if the value left is lesser than 0. if it is not we increase the minimum fixed payment by 10 and repeat the process until we have a balance that is lesser than or equal to zero.

That is the modification I made to the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM