Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

Hello, I have been having trouble with Problem Set 1 Problem number 2, in which you are supposed to write a program to calculate the fixed monthly payments needed to pay off a credit card debt within 12 months. I understand most of the solution to the problem, however, I don't understand the need for a nested while loop in order to execute the program; it seems redundant to me. However, whenever I try to run the program with only one while loop, it doesn't work as planned. Can someone please explain the purpose of the two while loops?

OpenStudy (unklerhaukus):

post code

OpenStudy (anonymous):

tysouza, Maybe it's easier if you consider the loops as 2 separate sections. The innermost section just calculates your balance over a year paying a certain monthly payment. The outermost section, determines the monthly payment amount to use, runs through the innermost and compares to see if you've reached a balance of zero. If you haven't, it loops through again with a different payment value, resets the values used in the inner loop, and tries again until you reach zero or a negative value. Hope this helps, wdh2

OpenStudy (anonymous):

How did you find the solution with the code? I see the solution but no code.

OpenStudy (anonymous):

This is the answer to the problem: # 6.00 PS1-B Solution # Determines fixed minimum monthly payment needed to finish paying off credit card debt in 1 year # Retrieve input initialBalance = float(raw_input("Enter the outstanding balance on your credit card: ")) interestRate = float(raw_input("Enter the annual credit card interest rate as a decimal: ")) # Initialize state variables monthlyPayment = 0 monthlyInterestRate = interestRate/12 balance = initialBalance # Test increasing amounts of monthlyPayment in increments of $100 # until it can be paid off in a year while balance > 0: monthlyPayment += 10 balance = initialBalance numMonths = 0 # Simulate passage of time until outstanding balance is paid off # Each iteration represents 1 month while numMonths < 12 and balance > 0: # Count this as a new month numMonths += 1 # Interest for the month interest = monthlyInterestRate * balance # Subtract monthly payment from outstanding balance balance -= monthlyPayment # Add interest balance += interest # Round final balance to 2 decimal places balance = round(balance,2) print "RESULT" print "Monthly payment to pay off debt in 1 year:", monthlyPayment print "Number of months needed:", numMonths print "Balance:",balance

OpenStudy (anonymous):

The reason is that the nested while loop serves to test whether the current payment will accomplish the goal. If you break the test in the nested while loop by not paying off your card and having a nonzero balance at the end of 12 months (the test will evaluate to FALSE) then you will exit and reinstantiate the balance to the "initialBalance" and start over with a different payment. The computer exits the nested loop and goes back out to the outer loop and starts over again. If you had only one you have to revamp this code completely. You need to test two things, whether you have paid it off and whether you have paid it off in time, which gives rise to the two while loops.

OpenStudy (whpalmer4):

The two while loops provide an easier way to solve the problem than writing a program to solve \[-12 p-66 i p-220 i^2 p-495 i^3 p-792 i^4 p-924 i^5 p-792 i^6 p-495 i^7 p\]\[-220 i^8 p-66 i^9 p-12 i^{10} p-i^{11} p+X+12 i X+66 i^2 X+220 i^3 X+495 i^4 X\]\[+792 i^5 X+924 i^6 X+792 i^7 X+495 i^8 X+220 i^9 X+66 i^{10} X+12 i^{11} X+i^{12} X=0\] :-) (\(p\) is monthly payment amount, \(i\) is monthly interest rate expressed as a decimal, and \(X\) is the starting balance)

OpenStudy (whpalmer4):

Stupid OpenStudy page design still doesn't show you the actual area you have to fit your formula when editing. All that stuff = 0. Solve for \(p\).

OpenStudy (anonymous):

Thank you all for your help! I understand the solution now.

OpenStudy (anonymous):

I have the same question. In the inner loop we already write the 2 conditions ( the duration and the balance). Why I should write another separate loop to check the balance? My mind cant get it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!