A community for students.
Here's the question you clicked on:
 0 viewing
MakaiM
 one year ago
Help needed with P1 Problem 2  infinite loop in my code...
MakaiM
 one year ago
Help needed with P1 Problem 2  infinite loop in my code...

This Question is Open

MakaiM
 one year ago
Best ResponseYou've already chosen the best response.0Can 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
 one year ago
Best ResponseYou've already chosen the best response.1I 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.