[Tutor] infinite while loop
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Oct 2 19:32:51 EDT 2021
On 02/10/2021 23:14, marcus.luetolf at bluewin.ch wrote:
> def payOff(balance):
> annualInterestRate = 0.2
> minimumFixedMonthlyPayment = 0
>
> while True:
> previousBalance = balance
> minimumFixedMonthlyPayment += 10
>
> for dummy_i in range(12):
> previousBalance = previousBalance - minimumFixedMonthlyPayment
> newBalance = round(previousBalance +
> (previousBalance*(annualInterestRate/12)),2)
> previousBalance = newBalance
> if newBalance <= 0:
> break
> if newBalance <= 0:
> break
>
> return( minimumFixedMonthlyPayment)
The double break is a bit ugly since they both test the same
condition(albeit under different circumstances). You could
eliminate the second break test and move the return
statement inside the for loop, replacing the first break.
Then as soon as the value drops to/below zero you will end
the function and return the chosen value.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list