Iteration and the Bank Account Program

Consider the bank account program to compute interest earned after two years:

principal=input('please enter the principal')
rate = input('please enter the rate')
oneYearInterest=principal*rate
oneYearPrincipal=principal+oneYearInterest
twoYearInterest=oneYearPrincipal*rate
twoYearPrincipal=oneYearPrincipal+twoYearInterest
print twoYearPrincipal

How can we improve this program so that it could tell us how much money a person will have after any number of years?

1. Have the end-user enter the number of years.


2. We can't use a different principal and interest variable for every year, since we don't know how many years. But we don't really need to! In the code above, just change the variable names oneYearPrincipal and twoYearPrincipal to principal. Does it work? How come?


3. Since we want to do something a number of times, we need a loop. What should be the limit variable of our loop? How many times do we want to compute interest and resulting principal? Which lines in the code above should be within the loop?


The beginning program and the iterative one are attached to this page. Check them out!



Č
ċ
ď
bank.py
(0k)
David Wolber,
Mar 31, 2009 10:01 AM
ċ
ď
bankloop.py
(0k)
David Wolber,
Mar 31, 2009 10:02 AM

Recent site activity