# [Tutor] Loop Question - Beginner Help (Thanks in Advance)

Dave Angel 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
> interest
> ##       rate, and the program will tell them what monthly payment will
> allow
> ##       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
> final
> ##          balance over 12 months.  But it somehow needs to be dynamic
> enough
> ##          to recognize that it will not take 12 months to pay off some
> values
> ##          if we only allow multiples of \$10.
>
> ##          I thought of scabbing something on the end that will check
> negative
> ##          balances and somehow revert to the previous month but there
> must be
> ##          a cleaner way of doing this.
>
> # code follows:
>
>
>
> 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
>
>     else:
>         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
final balance.

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

--

DaveA

```