anonymous
  • anonymous
Help needed with P1 Problem 2 - infinite loop in my code...
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
Can anyone find where the infinite loop is? I can't seem to locate it! ## Problem Set 1, Problem 2 outstandingBalance = float(raw_input("Please enter the outstanding balance on the card: ")) interestRate = float(raw_input("Please enter the annual interest rate on the card: ")) monthlyInterestRate = interestRate/12.0 paymentAmount = 10 monthsRequired = 1 while paymentAmount<=outstandingBalance: while monthsRequired<=12 and outstandingBalance>0: outstandingBalance = outstandingBalance * (1+monthlyInterestRate)-paymentAmount monthsRequired = monthsRequired +1 paymentAmount = paymentAmount+10 print ("Monthly payment to pay off debt in 1 year: " + str(paymentAmount)) print ("Number of months needed: " + str(monthsRequired)) print ("Balance: " + str(outstandingBalance))
eSpeX
  • eSpeX
I believe your problem lies here: while paymentAmount<=outstandingBalance: while monthsRequired<=12 and outstandingBalance>0: I put in 1000 and .1 and stepped through the sequence 12 times. On the 12th run your values are: paymentAmount: 120 outstandingBalance: 416.901495833 monthsRequired: 12 As you can see, the paymentAmount is still less than the outstandingBalance, the outer loop is true, however the months required are about to be greater than 12 and thus the inner loop is false. This causes the infinite loop because it is never able to break out of both while loops.

Looking for something else?

Not the answer you are looking for? Search for more explanations.