09 Checkpoint: Text Files

Purpose

Check your understanding of text files and lists by writing a program that reads the contents of a text file into a list and then changes some of the values in the list.

Helpful Documentation

Assignment

You must do the following to setup VS Code so that your program can read from a text file:

  1. Download the provinces.txt file and save it in the same folder where you will save your Python program.
  2. Using VS Code, open the folder where you saved the provinces.txt file by doing the following:
    • On a computer running the Mac OSX operating system:
      1. Click the "File" menu, then "Open Folder..."
      2. Find and select the folder where you saved the provinces.txt file.
      3. Click the "Open" button.
    • On a computer running the Windows operating system:
      1. Click the "File" menu, then "Open Folder..."
      2. Find and select the folder where you saved the provinces.txt file.
      3. Click the "Select Folder" button.

Now that you have correctly setup VS Code so that your program can read the provinces.txt file, open that file in VS Code and examine it. Notice that the file contains a list of names of Canadian Provinces. Notice also that the file contains "AB" several times which is an abbreviation for Alberta.

Write a Python program named provinces.py that reads the contents of provinces.txt into a list and then modifies the list. Your program must do the following:

  1. Open the provinces.txt file for reading.
  2. Read the contents of the file into a list where each line of text in the file is stored in a separate element in the list.
  3. Print the entire list.
  4. Remove the first element from the list.
  5. Remove the last element from the list.
  6. Replace all occurrences of "AB" in the list with "Alberta".
  7. Count the number of elements that are "Alberta" and print that number.

Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. Run your program and ensure that your program’s output matches the output below. (Your program may wrap the province names differently.)
    > python provinces.py
    ['Ontario', 'Prince Edward Island', 'Ontario', 'Quebec',
    'Saskatchewan', 'Alberta', 'Nova Scotia', 'Alberta',
    'Northwest Territories', 'Saskatchewan', 'Nunavut',
    'Nova Scotia', 'Prince Edward Island', 'Alberta',
    'Nova Scotia', 'Nova Scotia', 'Prince Edward Island',
    'British Columbia', 'Ontario', 'Ontario',
    'Newfoundland and Labrador', 'Ontario', 'Ontario',
    'Saskatchewan', 'Nova Scotia', 'Prince Edward Island',
    'Saskatchewan', 'Ontario', 'Newfoundland and Labrador',
    'Ontario', 'British Columbia', 'Manitoba', 'Ontario',
    'Alberta', 'Saskatchewan', 'Ontario', 'Yukon', 'Ontario',
    'New Brunswick', 'British Columbia', 'Manitoba', 'Yukon',
    'British Columbia', 'Manitoba', 'Yukon',
    'Newfoundland and Labrador', 'Ontario', 'Yukon', 'Ontario',
    'Alberta', 'Nova Scotia', 'Newfoundland and Labrador',
    'Yukon', 'Nunavut', 'Northwest Territories', 'Nunavut',
    'Yukon', 'British Columbia', 'Ontario', 'Alberta',
    'Saskatchewan', 'Prince Edward Island', 'Saskatchewan',
    'Prince Edward Island', 'Alberta', 'Ontario', 'Alberta',
    'Manitoba', 'Alberta', 'British Columbia']
    
    Alberta occurs 9 times in the modified list.

Sample Solution

When your program is finished, view the sample solution for this assignment to compare your solution to that one. Before looking at the sample solution, you should work to complete this checkpoint program. However, if you have worked on it for at least an hour and are still having problems, feel free to use the sample solution to help you finish your program.

Submission

When complete, report your progress in the associated I‑Learn quiz.