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:

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):

  1. Input Section:
  2. Computation:
  3. Output:

Second Refinement (natural language pseudocode):

  1. Input Section:
  2. Computation:
  3. Output:

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.

Data Table:

Variables:

Constants:

Sample I/O:

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: 922001
CMPT 140 Fall 2006
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.