It’s time to work with sequences! Files, lists and strings…oh my!

Programmer joke: Why do programmers always mix up Halloween and Christmas? Because Oct 31 equals Dec 25!

Your Assignment

You are given a small text file containing financial data. This data was created using a modified version of the example. Your job is to write a program to read in this text file containing data and print out its information in a clear, human-readable fashion.

You will use indexing (maybe slicing), string methods, file input, string-to-numerical conversions and Python string formatting (as well as concepts learned in past weeks).

Your program is required to do the following:

  1. Print a short introduction about what this program does.

  2. Ask for a filename to open for reading.

  3. Open the file using the given filename, and then read in the text file. It has a specific format:

    • Line 1 always has the format years <integer value>, e.g. years 10.

    • Line 2 always has the format principal <decimal value>, e.g. principal 1000.00

    • Line 3 always has the format interest <decimal value>, e.g. principal 0.035

    • Line 4, and all remaining lines of the file will be just a decimal number, one per line, e.g.:


    Hint: Download and place data.txt in the same location as your Python program. That way, you avoid any issues with specifying pathnames to files and all the nuances that involves.

  4. After reading the file, I want you to extract the information about the years, principal, interest and all the balances for each year of accural of interest, stored in variables in your program for later use. Expect to convert those extracted bits of information into the right Python data types (whether they are a list of float numbers, single float or int values, etc.)

  5. Using the extracted information, you need to print out a nice table. Notice that the decimal numbers have two places of precision, monetary values have a dollar sign for U.S. Dollars, and the percentage rate is printed as a percentage value, not a basic decimal number. You will need to use Python string formatting to format the table nicely.

    This program reads in financial information from a file
    and prints it neatly to the user's screen.
    Please enter the filename of the financial data: data.txt
    The initial principal is: $42420.00
    Annual percentage rate is: 4.2%
    Length of the term (years): 10
    Year    Value
      0     $42420.00
      1     $44201.64
      2     $46058.11
      3     $47992.55
      4     $50008.24
      5     $52108.58
      6     $54297.14
      7     $56577.62
      8     $58953.88
      9     $61429.95
     10     $64010.00
  6. Once you are finished reading from the file, you need to properly close your file.

Submission Requirements

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Submit a file named

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Sequences
    This lab...
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): Unclear parts of your code can receive point deductions, so be judicious about using comments. Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.


The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Here’s a brief grading rubric:
  • Syntax errors: -30 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • Missing main() function: -10 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Missing introduction when your program runs: -10 points
  • Not asking for a filename to read: -10 points
  • Not properly opening and closing a file for reading: -10 points
  • Not using techniques taught in class to extract information from the file you are reading: -20 points
  • Your printed table of information is not properly formatted (and it must use Python string formatting techniques): -20 points.
Extra Credit:

There is no explicit extra credit for this assignment (if it changes, I will let you know).

I will allow you to add something creative and interesting to your program for extra credit. Your extra credit additions needs to be relevant to the techniques we are learning. You are not guaranteed to receive extra credit, it will be up to the lab TA to decide. If your extra credit code is not clearly documented using comments, you risk losing points instead of receiving bonuses.

Note: Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.

Note 2: There may be additional criteria for grading, this is just a summary of the major rubric items.