[Tutor] Loop Question - Beginner Help (Thanks in Advance)
d at davea.name
Wed May 30 03:44:14 CEST 2012
On 05/29/2012 08:46 PM, Joseph Rishe wrote:
> #Operating System - Mac OS X 10.6.8
> #Python Version - Python 2.6.6
> ##Goal: I am creating a program to calculate credit card payments.
> ## The user should be able to put in a credit card balance and
> ## rate, and the program will tell them what monthly payment will
> ## them to pay off the card in 1 year. Note: it should only be
> ## increments of $10.
> ##Problem: The program returns incorrent values for some inputs.
> ## ie ($1200 balance and 18% interest returns a value of $120.01
> ## for a monthly payment. Which is fine, but it only takes 11
> ## months to pay off that card, not 12.
> ## So, I need to try different payment values and play out the
> ## balance over 12 months. But it somehow needs to be dynamic
> ## to recognize that it will not take 12 months to pay off some
> ## if we only allow multiples of $10.
> ## I thought of scabbing something on the end that will check
> ## balances and somehow revert to the previous month but there
> must be
> ## a cleaner way of doing this.
> # code follows:
> print "This will help you pay off your credit card in under 1 year."
> obalance = float(raw_input("What's your balance?"))
> #takes user input for balance
> yrate = float(raw_input("What's your interest rate?"))
> #takes user interest rate
> mrate = yrate / 12.0
> #creates monthly rate
> cbalance = obalance
> #this will be experimental balance based on some monthly payment
> gbalance = 0.0
> #this is our goal balance
> incrementpayment = 10.0
> #initial monthly payment
> x = 1
> #our intitial month count
> while cbalance > gbalance:
> # while our experimental balance is greater than our goal of 0:
> for i in range (1,x+12):
> ppayment = incrementpayment - (cbalance * mrate)
> #amount of payment going to principle
> cbalance = cbalance - ppayment
> #new experimental balance after payment is applied
> if cbalance > gbalance:
> cbalance = obalance
> incrementpayment = incrementpayment + 10.0
> #resets experimental balance if monthly payment is not enough to
> reach zero balance
> print "RESULT"
> print "Months to pay off: ",i
> print "Monthly Payment: $",incrementpayment
> print "Ending Balance: $",cbalance
If you don't mind me saying so, you're trying to write BASIC code in
Python. if you want to solve this, decompose it into pieces that you
can debug individually.
Start by writing small functions that you're confident in, and the rest
will fall into place.
First function I'd write would take a given original principle, an
interest rate, a number of months, and payment amount. It'd return the
Then your other function is just a loop that calls that function with
upwardly varying payment amounts till the final balance is negative.
Incidentally, if your user types in interest rate as a percentage,
you'll need to scale it by 100. If he enters 6, the mrate should be 0.005
More information about the Tutor