Last active
May 30, 2016 14:54
-
-
Save CodeWithCory/a2fdef5abf608eb1266771f036474472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Cory Leigh Rahman | |
#GEOG469: Python with Dr. Bortolot | |
#Python 2.7.8 | |
#This program estimates population over a specified number of years, | |
#then gives you the minimum and maximum ending ending populations over | |
#a specified number of iterations | |
# | |
#The formula used is as follows: | |
#starting population + (reproduction rate * starting population) + (death rate * starting population) - number hunted | |
import random | |
print "********************************" | |
print "Cory's Population Growth Modeler" | |
print "********************************" | |
def about_program(): | |
print "This program estimates population over a specified number of years," | |
print "then gives you the minimum and maximum ending ending populations over" | |
print "a specified number of iterations" | |
print "The formula used is as follows:" | |
print "starting population + (reproduction rate * starting population) +" | |
print "(death rate * starting population) - number hunted" | |
print "Code written by Cory Rahman" | |
print "Dr. Bortolot's Python class" | |
print "James Madison University" | |
startover() | |
def input_values(): | |
print "***Using Input Values***" | |
min_val=1000000000000 | |
max_val=-1000000000000 | |
popn2=raw_input("Starting Population?: ") | |
popn2_int=int(popn2) | |
num_years=raw_input("Estimate the population after how many years?: ") | |
num_years_int=int(num_years) | |
num_years_int_correct=num_years_int+1 | |
num_iterations=raw_input("How many iterations would you like to run?: ") | |
num_iterations_int=int(num_iterations) | |
num_iterations_int_correct=num_iterations_int+1 | |
print "Random variable extents:" | |
r1=raw_input("Minimum reproduction rate?: ") | |
r1_float=float(r1) | |
r2=raw_input("Maximum reproduction rate?: ") | |
r2_float=float(r2) | |
d1=raw_input("Minimum death rate?: ") | |
d1_float=float(d1) | |
d2=raw_input("Maximum death rate?: ") | |
d2_float=float(d2) | |
h1=raw_input("Minimum hunted per year?: ") | |
h1_float=float(h1) | |
h2=raw_input("Maximum hunted per year?: ") | |
h2_float=float(h2) | |
event_chance=raw_input("Percent chance of an event occuring that changes population?: ") | |
event_chance_int=int(event_chance) | |
popchange=raw_input("What number shall we multiply the population by for this event?: ") | |
popchange_float=float(popchange) | |
event_counter=0 | |
for iterations in range (0,num_iterations_int_correct): | |
popn=popn2_int | |
val=0 | |
for year in range (0,num_years_int_correct): | |
r=random.uniform(r1_float,r2_float) | |
d=random.uniform(d1_float,d2_float) | |
h=random.uniform(h1_float,h2_float) | |
event=random.randint(event_chance_int,101) | |
popl=popn | |
popn=popl+r*popl-d*popl-h | |
if popn<0: | |
popn=0 | |
if year==20: | |
val=popn | |
if event==100: | |
popn=popn*popchange_float | |
event_counter+=1 | |
if val<min_val: | |
min_val=val | |
if val>max_val: | |
max_val=val | |
min_val_int=int(min_val) | |
max_val_int=int(max_val) | |
average_val=int((min_val+max_val)/2) | |
print "Results:" | |
print "Out of",num_iterations_int,"iterations:" | |
print "Number of events occured: ", event_counter | |
print "Minimum value:",min_val_int | |
print "Maximum value:",max_val_int | |
print "Average:",average_val | |
startover() | |
def default_values(): | |
print "***Using Default Values***" | |
min_val=1000000000000 | |
max_val=-1000000000000 | |
event_counter=0 | |
for iterations in range (0,101): | |
popn=3000 | |
val=0 | |
for year in range (0,21): | |
r=random.uniform(.9,1.4) | |
d=random.uniform(.9,1.1) | |
h=random.uniform(100,200) | |
event=random.randint(0,101) | |
popl=popn | |
popn=popl+r*popl-d*popl-h | |
if popn<0: | |
popn=0 | |
if year==20: | |
val=popn | |
if event==100: | |
popn=popn*.9 | |
event_counter+=1 | |
if val<min_val: | |
min_val=val | |
if val>max_val: | |
max_val=val | |
min_val_int=int(min_val) | |
max_val_int=int(max_val) | |
average_val=int((min_val+max_val)/2) | |
print "Results:" | |
print "Out of 100 iterations:" | |
print "Number of events occured: ", event_counter | |
print "Minimum value:",min_val_int | |
print "Maximum value:",max_val_int | |
print "Average:", average_val | |
startover() | |
def end(): | |
print "Thanks for using Cory's Population Growth Model! Goodbye." | |
def startover(): | |
startoverx=raw_input("Start Over? Yes(1) or No, End Program(2): ") | |
startover_int=float(startoverx) | |
if startover_int==1: | |
print "**Starting Over**" | |
start() | |
elif startover_int==2: | |
end() | |
else: | |
print "*Invalid Input*" | |
startover() | |
def start(): | |
event_counter=0 | |
q1=raw_input("----------MENU----------" | |
'\n' "(1) Use Default Values" | |
'\n' "(2) Enter Your Own Values" | |
'\n' "(3) About" | |
'\n' "(4) End Program" | |
'\n' "Please select from the above:") | |
q1_int=int(q1) | |
if q1_int==1: | |
default_values() | |
elif q1_int==2: | |
input_values() | |
elif q1_int==3: | |
about_program() | |
elif q1_int==4: | |
end() | |
else: | |
print "*Invalid Input*" | |
start() | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment