Python Program: A triple quoted string after the def, docstring , is required fo
ID: 3772108 • Letter: P
Question
Python Program:
A triple quoted string after the def, docstring, is required for your function documentation. Your program also expected to have an exception handle invalid input. You need put your function in a loop.
4. Program: Work with Census Data
Use urpop0090.txt (http://www.census.gov/population/www/censusdata/files/urpop0090.txt) , that is download from censussdata website to find out the total population with the minimum and maximum. The census file is a text file with data for the 10-year census from 1900 to 1990 (e.g., 1900, 1910, 1920, …). It has population data for each state as well as regional and overall data. Each state is on its own line, but the data are grouped so that only three decades of data are on each line – complication the task of extracting the data. In addition, the data are further broken down into total, urban, rural, and percentages. Wrap function call in a loop so that it can be continuous check for different year.
Explanation / Answer
I am really confused about what you want in the solution. For now I have parsed the file and printed the states with max and min population for 1990 census. If you want something else, I can edit my answer
def foo():
f = open('urpop.txt', 'r')
arr =[]
count = 0
maxPopulation = 0
minPopulation = 0
maxPopulationState = ""
minPopulationState = ""
for line in f:
collapsedLine = ' '.join(line.split())
arr = collapsedLine.split(' ')
state = ""
count = 0
for word in arr:
if word[0].isalpha() and word[0] != 'r':
state = state + " " + word
count+=1
state = state.strip()
population = int(arr[count].replace(',', ''))
if population > maxPopulation:
maxPopulation = population
maxPopulationState = state
if state == "Northeast Region":
minPopulation = population
minPopulationState = state
if population < minPopulation:
minPopulation = population
minPopulationState = state
print "Max Population state in 1990 is", maxPopulationState
print "Min Population state in 1990 is", minPopulationState
f.close()
foo()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.