CS110 Assignment#3
------------------
In this exercise we were asked to write Pyhton programs to do three kinds
of population projections. We used US population data as a specific example.
First, we were aske dto project the US population in 2050, given the population
in 2009 based on three different growth rates (0.8%, 0.9%, and 1.0%). Next, we
were asked to project the year in which the US population will exceed
1/2 billion (again using three grothw rates). Lastly, we were asked to project
the year when the US population would double. The data I obtained from my programs
is shown below.
RESULTS
-------
The following table shows the population data computed by my programs.
Growth Rate Pop. in 2050 Year Exceed 1/2B Year Double
-----------------------------------------------------
0.8 423579167 2071 2096
0.9 441154375 2064 2087
1.0 459440314 2059 2079
I wrote three Python programs, one for each problems. Each program was defined
as a Python function. As required, each function uses the 'input' command to
prompt the user to enter the specific data required. This makes the functions general
so one can use them over and over again for different sets of data. In this
exercise, I used them with different growth rates to get the above projections.
Below, First I give a printout of my Python programs. This is followed by an
actual prinout of sampe runs for each set of data.
1. Python Programs
------------------
##Write Python programs to make the following population projections:
## (1) What will the US population be in the year 2050?
## (2) In which year will the US Population exceed 1/2 billion?
## (3) In which year will the US population double?
##
##Write a separate program to answer each of the questions posed above.
##You may use the example program in Chapter 4 (page 90) as your starting
##template.
##
##Your program should input the Current population, growth rate,
##and the current year (if needed), and any other input required
##to answer the questions. You can use the figure: 305,529,237 as
##the population of the United States on January 1, 2009. Use a
##growth rate of 0.9%. Additionally, show the outputs of all three
##programs if the growth rate changed to: 0.8% and 1.0%.
def projectPop():
# What will the US population be in 2050?
# First, input the current year, population of US, and growth rate
currentYear = input("Enter the current year:")
usPop = input("Enter pupulation of US in "+str(currentYear)+":")
growthRate = input("Enter the population growth rate:")/100.0
endYear = 2050
# Compute pop for 2050
for year in range(currentYear+1, endYear+1):
usPop = usPop * (1.0 + growthRate)
# Output results
print "In the year",endYear,"the US population will be:", usPop
def projectHalfBill():
# Which year the population will increase to 1/2 billion?
# First, input the current year, population of US, and growth rate
currentYear = input("Enter the current year:")
usPop = input("Enter pupulation of US in "+str(currentYear)+":")
growthRate = input("Enter the population growth rate:")/100.0
targetYear = currentYear
# Compute year when population will exceed 1/2 billion
while usPop < 500000000:
usPop = usPop * (1.0 + growthRate)
targetYear = targetYear + 1
# Output results
print "In the year",targetYear,"the US population will be:", usPop
def projectDouble():
# Which year the population will double
# First, input the current year, population of US, and growth rate
currentYear = input("Enter the current year:")
usPop = input("Enter pupulation of US in "+str(currentYear)+":")
growthRate = input("Enter the population growth rate:")/100.0
targetPop = 2*usPop
targetYear = currentYear
# Compute year when population will double
while usPop < targetPop:
usPop = usPop * (1.0 + growthRate)
targetYear = targetYear + 1
# Output results
print "In the year",targetYear,"the US population will be:", usPop
2. Sample Runs
--------------
2.1 Population Projection for 2050
----------------------------------
>>> projectPop()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:.8
In the year 2050 the US population will be: 423579167.038
>>> projectPop()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:0.9
In the year 2050 the US population will be: 441154375.666
>>> projectPop()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:1.0
In the year 2050 the US population will be: 459440314.525
>>>
>>>
2.2 Population Projection 1/2 Billion
-------------------------------------
>>> projectHalfBill()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:0.8
In the year 2071 the US population will be: 500732484.003
>>> projectHalfBill()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:.9
In the year 2064 the US population will be: 500111589.035
>>> projectHalfBill()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:1.0
In the year 2059 the US population will be: 502483105.674
>>>
>>>
2.3 Population Projection for Doubling
--------------------------------------
>>> projectDouble()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:0.8
In the year 2096 the US population will be: 611109547.741
>>> projectDouble()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:0.9
In the year 2087 the US population will be: 614559224.756
>>> projectDouble()
Enter the current year:2009
Enter pupulation of US in 2009:305529237
Enter the population growth rate:1.0
In the year 2079 the US population will be: 613124880.785
>>>