Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

Help needed with P1 Problem 2 - infinite loop in my code...

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

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))
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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Not the answer you are looking for?

Search for more explanations.

Ask your own question