i'm learning python 2.5.4 programming language using mit ocw scholar course 6.00.
i have written program, same input, program's output differs output mit people have shown program produce.
i wish write python 2.5.4 program current outstanding balance , annual interest rate input user, , uses bisection search method find minimum fixed minimum monthly payment cause balance fall below 0 within 12 months. program displays right monthly amount, number of months needed claer outstanding amount, , final balance.
here's code:
# start of code balance = float(raw_input('enter outstanding balance on credit card: ')) interestrate = float(raw_input('enter annual credit card interest rate decimal: ')) lower = balance / 12.0 upper = ( balance * ( 1 + interestrate / 12.0 ) ** 12.0 ) / 12.0 payment = ( lower + upper ) / 2.0 currentbalance = balance while currentbalance > 0: months in range(1, 13): currentbalance = currentbalance * ( 1 + interestrate / 12.0 ) - payment if currentbalance <= 0: break if currentbalance <= 0: break else: lower = payment payment = ( lower + upper ) / 2.0 currentbalance = balance print 'result' print 'monthly payment pay off debt in 1 year:', '$' + str(round(payment, 2)) print 'number of months needed:', months print 'balance:', '$' + str(round(currentbalance, 2)) #end of code
does code desired job? if not, lies problem? and, how fix it? if program right, in ways can improved?
please bear in mind i'm beginner.
regards.
you have couple of typos (which may have been introduced when copying posting):
you split
balance on credit
/card:
across line-ending, give syntaxerrorcurrentbalance = currentbalance * ( 1 + interestrate / 12.0 ) payment
missing-
operator beforepayment
the first logical error is
if currentbalance <= 0: break
... stop find higher needed payment; ever increase possible payment values (lower = payment
), never decrease (you should have clause leading upper = payment
, otherwise bisection search lopsided).
once that, have add new test know when stop looping. target accuracy - payment nearest cent? how know when have found it?
my other suggestion improve code organization defining operations functions - get_float(prompt)
, final_balance(current_balance, interest_rate, num_payments)
obvious candidates.
Comments
Post a Comment