0

I am creating a program that will compute the lowest monthly payment to be paid for a credit card, given a certain credit card balance and interest rate. The time frame for the payoff is 12 months and the monthly payment has to be accurate down to the nearest penny, using bisection search.

I was able to get the answer, the problem is that I couldn't get my while loop to quit once the monthly payment was calculated to the nearest cent, so I had to make an infinite while loop that has a elif statement at the bottom of the while loop that quits for me. I was wondering if anyone can figure out what condition to give the while loop so it will quit on its own. Also I just started learning python a week ago, and want some advice on how good/bad my code is. Any ideas?

# random balance
balance = 999999
# random interest rate
annualInterestRate = 0.18 
# assign balance to another variable that will undergo the testing
balance_tested = balance
# bounds of bisection search
low = (balance / 12.0) 
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0)
# start month
month = 1
monthlyPayment = (low + high) / 2.0 #Averages out the bounds to meet in the middle
while abs(balance_tested != 0): #While loop that I can't get right, just made it to run infinitely
 balance_tested = balance #Resets balance being tested back to original balance
 monthlyPayment = (low + high) / 2.0 #Bisection search recalculates
 month = 1 #Month reset back to 1
 while month <= 12: #Loops through all 12 months with the payments being made and interested getting added
 balance_tested = (balance_tested - monthlyPayment)
 balance_tested += (balance_tested * (annualInterestRate/12))
 month += 1
 print "Balance Remaining: %.20f" % balance_tested
 if balance_tested < 0: #If the bisection search guesses to high, decreases the high bound
 high = monthlyPayment
 elif balance_tested <= 0.01: #Conditional statement that stops the testing if the balance gets paid off to the cent
 break
 else: #If bisection search guesses to low, increases low bound
 low = monthlyPayment
 print "Monthly Payment: %.2f" % monthlyPayment
print "Lowest Payment: %.2f" % monthlyPayment
Hugh Bothwell
57k9 gold badges91 silver badges103 bronze badges
asked Feb 26, 2014 at 19:54
3
  • 1
    You have to use bisect? An analytic answer is possible. Commented Feb 26, 2014 at 19:58
  • but... don't you already have the condition right there? Just do while balance_tested <= 0.01: Commented Feb 26, 2014 at 20:00
  • Had to use bisect since this particular problem asked me to Commented Feb 26, 2014 at 20:22

3 Answers 3

1

Is there any reason you're using the break statement instead of just putting that as the condition for your while loop?

balance = 999999 
annualInterestRate = 0.18 
balance_tested = balance
low = (balance / 12.0) #Lower bound of bisection search
high = ((balance * (1 + (annualInterestRate/12.0))**12)/12.0) 
month = 1 
monthlyPayment = (low + high) / 2.0 
while not (balance_tested <= 0.01): 
 balance_tested = balance 
 monthlyPayment = (low + high) / 2.0 
 month = 1 
 while month <= 12: 
 balance_tested = (balance_tested - monthlyPayment)
 balance_tested += (balance_tested * (annualInterestRate/12))
 month += 1
 print "Balance Remaining: %.20f" % balance_tested
 if balance_tested < 0: 
 high = monthlyPayment
 else: 
 low = monthlyPayment
 print "Monthly Payment: %.2f" % monthlyPayment
print "Lowest Payment: %.2f" % monthlyPayment
answered Feb 26, 2014 at 20:00
Sign up to request clarification or add additional context in comments.

Comments

0

You already have the condition, why not put that for the while statement instead?

while not balance_tested <= 0.01:
 # etc.
answered Feb 26, 2014 at 20:01

1 Comment

That's the condition to break, so you'd want the opposite.
0
while abs(balance_tested != 0):

will stay in the loop if the balance_tested becomes negative. Make this

while balance_tested >= 0.1:

This will automatically handle the rounding when it drops below a penny.

answered Feb 26, 2014 at 20:03

3 Comments

Oh wow I guess I was just to tired/too far into writing all of this that I just didn't see it. I was only using bisection search because I am doing a tutorial that asked me to do so. Do you see anything bad as to how I wrote the program that could make it better?
@nolls1 When setting the high and the low I would make the if test on the value of high or low rather than balance_tested. That is if montlyPayment > high: high = monthlyPayment
Please ignore the previous comment. I misread your use of high and low to be highest and lowest over the months. However the if test to redo high and low is wrong as when balance_test is < 0 you should should redo the monthlyPayment to be the actual remainder and break out of the monthly loop (which then leaves the outer loop) with a print. Also you should recalculate low and high for each month to be balance_tested/monthsremaining and high to be balance_tested as affected by months remaining. The final calculation (of month 12) has a denominator of 1 so low pays off the loan (if you want to)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.