Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

MakaiM Group Title

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

  • one year ago
  • one year ago

  • This Question is Open
  1. MakaiM Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  2. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
    • Attachments:

See more questions >>>

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

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.