[Tutor] Loop Question - Beginner Help (Thanks in Advance)
Joseph Rishe
jdrishe at gmail.com
Wed May 30 02:46:43 CEST 2012
#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:
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
else:
print "RESULT"
print "Months to pay off: ",i
print "Monthly Payment: $",incrementpayment
print "Ending Balance: $",cbalance
--
Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120529/37d7741e/attachment.html>
More information about the Tutor
mailing list