gab - 9 months ago 51

Python Question

Below I have a piece of code which calculates credit card balance, but it doesn't work when

`balance`

`balance=9999999999`

`balance = 9999999999`

annualInterestRate = 0.2

monthlyPayment = 0

monthlyInterestRate = annualInterestRate /12

newbalance = balance

month = 0

while newbalance > 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)))

My theory is that

`while newbalance > 0`

is causing the infinite loop, because newbalance is always larger than 0.

How can I change this

`while`

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.

Answer

The bisection method will execute much quicker if you're allowed to use it in your assignment. Will not help you though, if you're required to increment the monthly payment by .01.

```
static_balance = balance
interest = (annualInterestRate/12)
epsilon = 0.01
lo = balance/12
hi = balance
while abs(balance) > epsilon:
balance = static_balance
min_pmt = (hi+lo)/2
for i in range(12):
balance -= min_pmt
balance *= 1+interest
if balance > 0:
lo = min_pmt
else:
hi = min_pmt
print("Lowest payment: ", round(min_pmt, 2))
```

Source (Stackoverflow)