[Tutor] home_finance.py

Christopher Spears cspears2002 at yahoo.com
Wed Sep 26 04:07:16 CEST 2007


I'm working on a problem in Chapter 5 of Core Python
Programming(2nd Edition).  I am supposed to write a
script that take an opening balance and a monthly
payment from the user.  The script then displays the
balance and payments like so:


Enter opening balance: 100
Enter monthly payment: 16.13
        Amount  Remaining
Pymnt#  Paid    Balance
0       0.00    100.00
1       16.13   83.87
2       16.13   67.74
3       16.13   51.61
4       16.13   35.48
5       16.13   19.35
6       16.13   3.22
7        3.22   0.00

Here is what I have written so far:

#!/usr/bin/env python

balance = float(raw_input("Enter opening balance: "))
payment = float(raw_input("Enter monthly payment: "))

print "\tAmount\tRemaining"
print "Pymnt#\tPaid\tBalance"

payment_num = 0
print "%d\t%.2f\t%.2f" % (payment_num,0,balance)

while balance >= 0:
    payment_num = payment_num + 1
    if balance > 0:
        balance = balance - payment
        print "%d\t%.2f\t%.2f" %
(payment_num,payment,balance)
    else:
        payment = balance
	balance = balance - payment
        print "%d\t%.2f\t%.2f" %
(payment_num,payment,balance)
	
This is what my script displays:

Enter opening balance: 100
Enter monthly payment: 16.13
        Amount  Remaining
Pymnt#  Paid    Balance
0       0.00    100.00
1       16.13   83.87
2       16.13   67.74
3       16.13   51.61
4       16.13   35.48
5       16.13   19.35
6       16.13   3.22
7       16.13   -12.91

I'm not sure how to solve this problem.  Apparently,
trying to catch the remaining balance when it becomes
negative doesn't work!


More information about the Tutor mailing list