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.
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 :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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 :)
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
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.