Why do I have an infinite loop in my code?
<p>Below I have a piece of code which calculates credit card balance, but it doesn't work when <code>balance</code> has an extreme value (such as <code>balance=9999999999</code>below). It throws the code through an infinite loop. I have a couple of theories as to how to fix this flaw, but don't know how to move forward with them. Here's my code:</p>

<pre><code>balance = 9999999999
annualInterestRate = 0.2
monthlyPayment = 0

monthlyInterestRate = annualInterestRate /12
newbalance = balance
month = 0

while newbalance &gt; 0:
    monthlyPayment += .1
    newbalance = balance

    for month in range(1,13):
        newbalance -= monthlyPayment
        newbalance += monthlyInterestRate * newbalance
        month += 1
print("Lowest Payment:" + str(round(monthlyPayment,2)))
</code></pre>

<p>My theory is that
<code>while newbalance &gt; 0</code>
is causing the infinite loop, because newbalance is always larger than 0. </p>

<p>How can I change this <code>while</code> loop so that it doesn't cause my code to run infinitely? </p>

<p>By the way:
With moderate numbers, the program runs for a long time and finally gives an answer. For the larger numbers, the program just keeps on going.</p>

<p>This loop is not infinite, but will take a long time to resolve.  For very large values of <code>balance</code>, <code>monthlyPayment</code> will have to get very large in order to drop it past zero.  </p>

python python-3.x infinite-loop