Question
the language is in Python please help
Instructions from your teacher. Instructions The US Population.txt file contains the midyear population of United States, in
Instructions from your teacher. Example Output Year 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 196
Instructions from your teacher. 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 198
Inbox (2,384) - eaperezreye@ualr.edu - UA Little Rock Mail @repl.it 8.23 - back to classroom run USPopulation.txt 7 main.py 1
back to classroom run USPopulation.txt 15 21 22 main.py 191141 193526 195576 197457 199399 201385 203984 206827 209284 211357
back to classroom run main.py USPopulation.txt 28 29 30 31 32 33 219760 222095 224567 227225 229466 231664 233792 235825 2379
0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''
Python version : 3.6
Python program to read the text file containing US population from year 1950 to 1990
and calculate the change in population and its percent change and calculate the average
population change, minimum and maximum population change and the corresponding years
'''

# specify the file name
file = 'USPopulation.txt'

# empty list to contain the population from 1950 to 1990
population = []
# open the file
with open(file) as fp:
   # read the contents of the file into the list with each line being an element of the list
   contents = fp.readlines()
   fp.close() # close the file
   # loop over the contents list
   for line in contents:
       # strip is used to remove leading and trailing whitespace from line
       # int is used to convert string to integer and multiply the result by 1000
       population.append(int(line.strip())*1000)
  
   # initialize the variables for average, maximum and minimum
   total_population_change = 0
   min_population_change = -1
   max_popuation_change = -1
   min_pop_change_year = 0
   max_pop_change_year = 0
  
   # display the header
   print('%-10s%-15s%-15s%-s' %('Year','Population','Change','Percent Change'))
   year = 1950 # start year
   # loop over the population list
   for i in range(len(population)):
       # if first record
       if i==0:
           print('%-10d%-15d%-15s%-s' %(year,population[i],'N/A','N/A'))
       else: # for rest of the records
           change = population[i] - population[i-1] # calculate the change in population
           change_percent =float(change*100)/(population[i-1]) # calculate percent change
           # display the record
           print('%-10d%-15d%-15d%-.2f%-s' %(year,population[i],change,change_percent,'%'))
           total_population_change += change # add the change to total_population_change
           # determine if change is the minimum population change
           if min_population_change == -1 or min_population_change > change:
               min_population_change = change
               min_pop_change_year = year
           # determine if change is the maximum population change  
           if max_popuation_change == -1 or max_popuation_change < change:
               max_popuation_change = change
               max_pop_change_year = year
       year += 1      
  
   # calculate the average population change
   avg_change = float(total_population_change)/(len(population)-1)
   # display the average, minimum and maximum population change
   print('\nAverage Change = %.1f' %avg_change)
   print('Minimum Change = %d (%d)' %(min_population_change,min_pop_change_year))  
   print('Maximum Change = %d (%d)' %(max_popuation_change,max_pop_change_year))

#end of program  

Code Screenshot:

!יים loco VoWNE Python version: 3.6 Python program to read the text file containing US population from year 1950 to 1990 and

# initialize the variables for average, maximum and minimum total_population change = 0 min_population change = -1 max_popuat

Output:

Input file:

14 15 151868 153982 3 156393 158956 161884 | 165069 168088 8 171187 174149 10 | 177135 11 179979 12 182992 | 185771 188483 19

Output:

Add a comment
Know the answer?
Add Answer to:
the language is in Python please help Instructions from your teacher. Instructions The US Population.txt file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT