A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

Another question about the second Set/second Credit Problem. Here you need to find the minimum monthly amount to cover the balance within a year. In the solution they nest one while loop inside another. I understand that this is the only way the program can work, but I'm having a hard time understanding the subordination relation between the loops. In itself the first while loop does not make sense to me.

  • This Question is Closed
  1. StephenCampbell
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Think about it like this. The first, outer loop is incrementally trying to find the first (lowest) monthly payment that will pay off the bill. It starts by, in effect, saying to the second loop "here, try $10 a month and see if that works". The second, inner loop then takes that $10 a month payment and works out what happens with interest, repayments etc over the course of a year, then tells the first loop whether the bill was paid off with that level of payment. If the bill is not paid off (obviously not, since $10 is nowhere near enough), the first loop increases the monthly payment by $10 and says to the second loop "OK, now try $20". This continues until the second loop reports that the monthly payment is enough to pay off the bill in one year. At this point, the first loop then exits as well because the statement "while balance > 0" is no longer true. Hope that helps :)

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you. It does help. I understand it in broad terms, but what I fail to see is these three points: 1.Why do we need to repeat the condition "while balance>0" in the second loop? If the loops are connected won't that condition carry over? 2. why do we need to add a term "balance" and equate it to "initialBalance", the parameter that controls the user's input. Why not simply use "initialBalance"? 3. Why do we need to mention that number of months=0 Thnak you,

  3. StephenCampbell
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Hi, 1. Remember in the task where it said "Print out the ... number of months (at most 12 and possibly less than 12).."? If the balance was not checked for > zero, the inner loop would run for a full 12 months straight. However, we need to check if it's paid off sooner than 12 months (in the event, it is), so we need check the balance in the second loop so that it exits before the full 12 months are up if the balance is paid off early. (I used a "for" loop for the second loop, but the principle is the same) 2. The inner loop changes the variable "balance" through each cycle to add on interest and subtract the monthly payment. By the end of the 12 month cycle of the inner loop, we hope that "balance is down to zero, or close to it. What happens if you just use "initial_balance"? Let's think about Test Case 1, where you have $1200 on your card. Ignoring interest for the time being, the first sim that the script will run will be £10 per month. After a year, $120 will have been be paid off; "initial_balance" at the year end will be $1080. This is greater than zero, so the main loop will run again, adding $10 to the monthly payment, and it will ask the second loop to run another year''s sim. But - oh-oh! - If it starts with "initial_balance", this is now $1080, not $1200, and the sums will be all wrong. So this is why we take a second variable into the inner loop to play with, so that "initial_balance" is left alone so that everything can be reset to its original state if an inner-loop sim doesn't work out. 3. This just initialises the variable and tells Python that it takes an integer. Starting with the "0" means that we can then use it to count the number of months elapsed in the inner loop (by adding 1 each cycle). We then use that variable to report back how many months it took to pay off the bill.

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you. This is very helpful.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.