Resources:
Python 3 Documentation Documentation
Python Lab 7 Materials
Files Needed: USPopulation.txt
The file contains the midyear population of the United States, in thousands,
during the years 1950 through 1990. The first line in the file contains the
population for 1950, the second line contains the population for 1951, and so forth.
Write a program that reads a file’s contents into a list. The program should then display the following:
The average annual change in population during the time period.
The year with the greatest increase in population during the time period.
The year with the smallest increase in population during the time period.
Code to open a file and read its contents into a list.
# Open the file for reading.
input_file = open('USPopulation.txt', 'r')
# Read all the lines in the file into a list.
yearly_population = input_file.readlines()
Pseudocode
Open File
Read in contents into a list
Convert each element in list into a numerical data type
Calculate the change in population size for each two years
Set initial values for comparisons
Evaluate the next element
Update as necessary
Calculate Average Chnange by dividing the total change by the number of years
# Setup variables
yearly_change = []
change = 0.0
total_change = 0.0
average_change = 0.0
greatest_increase = 0.0
smallest_increase = 0.0
greatest_year = 0
smallest_year = 0
# Constant for the base year
BASE_YEAR = 1950
Conditions of Satisfactions
The average annual change in population during the time period is 2,443.88
The year with the greatest increase in population was 1955
The year with the smallest increase in population was 1967