[Tutor] infinite while loop

Dennis Lee Bieber wlfraed at ix.netcom.com
Sat Oct 2 13:22:52 EDT 2021


	Please ensure you reply to the LIST -- not to individuals.

On Sat, 2 Oct 2021 11:13:53 +0200, Marcus Lütolf
<marcus.luetolf at bluewin.ch> declaimed the following:

	What, exactly, are you attempting to calculate? The minimum payment
that will pay off a loan in 12 months?


>Thank you. 
>The previousBalance is set back to 1000 after each run oft he for loop intentionally for the
>following for loops should run with minimumFixedMonthlyPayment each time increased by 10
>til previousBalance get's negative.
>

	The for loop is, I presume, supposed to span 12 months -- one year. BUT
you are resetting "previousBalance" (which is a poor name, as it really is
the /remainingBalance/) to 1000 at the END of EACH MONTH -- so the
remaining balance never decreases inside the for loop. Furthermore, you are
ALSO changing (incrementing) the monthly payment EACH MONTH -- you do not
have a "fixed monthly payment" inside the for loop.


	BOTH the "fixed payment" and the initial balance need to be set BETWEEN
the "while" and the "for" loops. The for loop should only adjust the
remaining balance by applying the fixed payment (deduction) for the month
and interest (addition) for that month. The order you do those could
differentiate between interest before payment, or interest after payment.

set payment to 0.0
while True:
	set remaining balance to initial balance
	increment payment to next value
	for _ in range(12):
		apply payment to remaining balance
		apply interest to remaining balance
		if remaining balance <= 0.0: break
	if remaining balance <= 0.0: break

NOTE: I'm using the "while True" with an "if ...: break" to avoid the need
to "prime" the while loop with...

set remaining balance to initial balance
while remaining balance > 0.0:
	set remaining balance to initial balance

... however it requires the two if/break lines as the first exits the month
loop, and the second exits the while loop.

	I'm not going to show the code, but will show the last cycle of running
my implementation of this iterative approach...

period: 11	balance: 14.934028775505954	payment: 90.0
current payment: 100.0
period: 0	balance: 915.0	payment: 100.0
period: 1	balance: 828.5833333333333	payment: 100.0
period: 2	balance: 740.7263888888888	payment: 100.0
period: 3	balance: 651.4051620370369	payment: 100.0
period: 4	balance: 560.5952480709875	payment: 100.0
period: 5	balance: 468.27183553883725	payment: 100.0
period: 6	balance: 374.4096994644845	payment: 100.0
period: 7	balance: 278.9831944555592	payment: 100.0
period: 8	balance: 181.9662476964852	payment: 100.0
period: 9	balance: 83.33235182475994	payment: 100.0
period: 10	balance: -16.94544231149406	payment: 100.0
>>> 

	With your payment incrementing by 10, my solution case only runs 11
periods (months) on payment 100. Payment 90 still owes ~15 on the 12th
period.

NOTE: that this (minimum payment to pay off a loan in n-periods) is just
one variant of "time value of money" equations, and all of those have
solutions that do NOT require a loop.

http://www.frickcpa.com/tvom/tvom_amort.asp

Not a loop in sight -- just a straight up exponential equation.

>>> initialBalance = 1000
>>> annualRate = 0.2
>>> numberYears = 1
>>> periodYear = 12
>>> numberPeriods = numberYears * periodYear
>>> periodRate = annualRate / periodYear
>>> 
>>> payment = initialBalance * (periodRate / 
... 		(1.0 - (1.0 + periodRate) ** (-numberPeriods)))
>>> payment
92.63450589708033
>>> 

	Note: If you round down (92.63) that payment you will owe a residual of
a few cents at the end of the year. Rounding up (92.64) will result in the
last payment being a touch smaller.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list