[Tutor] home_finance.py

Ricardo Aráoz ricaraoz at gmail.com
Wed Sep 26 13:30:34 CEST 2007


Eric Walker wrote:
> I think you need to check to see if the remaining amount is less than
> the payment amount.
> 
> Eric...

Or just subtract the minimum between the remaining amount and the
payment amount. See below

> 
> */Christopher Spears <cspears2002 at yahoo.com>/* wrote:
> 
>     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
Instead of this ^^^ line you should have :
      balance = balance - min(balance, payment)
And I guess that would be it.

>     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!
>     _______________________________________________
>     Tutor maillist - Tutor at python.org
>     http://mail.python.org/mailman/listinfo/tutor
> 
> 
> ------------------------------------------------------------------------
> Luggage? GPS? Comic books?
> Check out fitting gifts for grads
> <http://us.rd.yahoo.com/evt=48249/*http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz>
> at Yahoo! Search.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list