CMPT14x Lab 00: Template Lab Solution
Problem:
Compute the amount there will be in my bank account after one year if
the interest is compounded quarterly and any withdrawals I make are entered
immediately after the interest is paid.
Problem Suitability:
The bank account example given above is well within the reach of a simple
calculator, and is therefore of somewhat marginal suitability for computer
solution. However, the solution does illustrate how a number of the
programming ideas discussed in chapter one are actually implemented in Python.
Problem Restatement:
- Given: A bank account, possibly containing money.
- To Do: Compute interest, make withdrawals every 3 months.
- Desired Result: Final balance.
- Formula: interest = principal * rate * time
Libraries:
This problem has both input and output requirements. It will, therefore
make use of the standard I/O Python functions input() and
print().
Because of the withdrawals, four separate simple interest
calculations will be made, rather than one compound interest calculation.
No mathematical or financial libraries are required.
Problem Refinement (natural language pseudocode):
- Input Section:
- obtain bank balance
- obtain interest rate
- obtain quarterly withdrawal amounts
- Computation:
- calculate interest each quarter
- subtract withdrawal each quarter
- Output:
Second Refinement (natural language pseudocode):
- Input Section:
- Ask the user for the initial balance,
and assign the value to a float variable
- Ask the user for the interest rate,
and assign the value to another float variable
- Computation:
- For loop: for each current quarter from 1 to 4:
- Ask user for widthdrawl amount, and assign the value to a real variable
- Compute the interest using interest = principal * rate * time
- Add this to the original balance: balance = balance + interest
- Subtract the withdrawal from last balance:
balance = balance - withdrawal
- Output:
- Print out the final balance
Pseudocode (third refinement; a fourth may also be necessary):
In this refinement, only portions of the code that involve several steps,
formulas, or computations (i.e. an algorithm) are detailed. In this example,
the last natural language refinement is followed very closely, so this
section might have been left out in this case, but it is included to make the
model for students to follow as complete as possible.
- For quarter in a range from 1 to 5 (excluding 5):
- prompt user, "what is widthdrawal amount?", store in widthdrawal
- interest := principal * time * rate
- balance := balance + interest
- balance := balance - widthdrawl
Data Table:
Variables:
- balance, rate, interest, withdrawal: all floats
- quarter: integer
Constants:
Sample I/O:
- What is the opening balance? $100
- What percent is the annual interest rate? 12.3
- How much do you wish to withdraw in quarter number 1? $20
- How much do you wish to withdraw in quarter number 2? $10
- How much do you wish to withdraw in quarter number 3? $50
- How much do you wish to withdraw in quarter number 4? $25
- The final balance in your account is $ 3.81478643166
User Manual:
Description:
Bank Interest is a small application designed to maintain a
simple bank account record. It allows the user to specify the opening balance,
and annual interest rate at the beginning of the program, and then to make
quarterly withdrawals. Interest is calculated on the balance before the
withdrawal.
Operation:
Locate the BankInterest.py file in your filesystem.
You may run the file directly by double-clicking on its icon (in Windows),
or by typing the name of the file at the operating system command prompt
(DOS or Unix).
Alternately, you may start the IDLE Python environment first, then select
the File/Open menu item and select the Python file to open. Then press F5
to run the program.
Upon starting, the program will prompt
What is the opening balance? $
Type in the number of dollars that the bank account had at
the start of the year.
Following this, the prompt
What percent is the annual interest rate?
will appear on the screen. Type in the interest rate as a percentage;
for example, type 6.5 if the interest rate is 6.5%. Do not type
the percent symbol.
Now, for each quarter, a message like
How much do you wish to withdraw in quarter number 1? $
will appear on the screen. Respond with the amount being withdrawn for that
quarter in the form 10.23. That is, give the number in decimal form and
without a dollar sign. The program will finally respond with a message like:
The final balance in your account is $103.47
Code:
"""A simple bank account example for our template lab writeup.
Name: Nellie Hacker
Student Number: 123456
CMPT 140 Fall 2007
Lab 00
Bank Interest Computation
"""
# Welcome blurb
print "This program computes interest on an account", \
"quarterly, and then allows for withdrawals."
print
# Get initial input
balance = input("What is the opening balance? $")
rate = input("What percent is the annual interest rate? ")
time = 0.25 #compound quarterly
# Input / computation for each quarter
for quarter in range(1,5):
withdrawal = input("How much do you wish to withdraw in quarter number " +
str(quarter) + "? $")
interest = balance * time * rate / 100
balance = balance + interest
balance = balance - withdrawal
# Final output
print "The final balance in your account is $", balance
# Pause before quitting so user can read output
raw_input()
NOTE: An actual lab assignment submission should include several
sample runs with different input.