17.4 Foxes vs. Rabbits
Imagine a population of Foxes and Rabbits living in an enclosed space. Each year, some animals are born and some die. Foxes will cause some of the rabbits to die. If the number of rabbits is too small, some foxes will die from starvation. In Python 3
This problem can be modeled in a program.
At the start of recorded time, there are a starting number of Foxes and Rabbits.
For example, at year 0
R[0] = 5,891 F[0] = 16
After one year passes, the number of animals will change. We need to compute the birth-death amount for the year.
R[1] = R[0] + Floor( R[0] * (A-B * F[0]))
The number of foxes changes the number of births and deaths.
A similar change in the foxes is caused by the number of rabbits.
F[1] = F[0] – Floor(F[0] * (G-S * R[0]))
A reasonable setting for these variables based on experimental data is.
A = 0.04 B = 0.0005 G = 0.2 S = 0.00005
After 1 year, the population looks like
R[1] = 5891+Floor(5891 * (0.04-0.0005 * 16)) = 6079 Rabbits
F[1] = 16 – Floor(16 * (0.2-0.00005 * 5891)) = 18 Foxes
Write a function bunnies(rabbits, foxes, years) that takes three inputs, the starting number of rabbits, starting number of foxes, and number of years to simulate. Return a List with two items. Put the final number of rabbits at index 0 and final number of foxes at index 1.
It is impossible to have a fractional fox or rabbit. The floor function rounds down to the nearest integer.
For example, bunnies(5891,16,1) will return [6079,18].
Develop a user interface that askes the user for the initial number of rabbits, initial number of foxes, and number of years to run the simulation. Print out the final populations.
Your function will be auto-tests. Make sure to use the if name=="main": command around your input/output so ZyBooks can test your function.
An example execution trace is provided below.
Welcome to Predator-Prey Model. Enter Initial Rabbit Population: 5891 Enter Initial Fox Population: 16 Enter Number of Years to Simulate: 99 After 99 years there will be 6484 rabbits. After 99 years there will be 144 foxes.
Solution:
import math #math module for using floor function
R=[]
F=[]
A=0.04
B=0.0005
G=0.2
S=0.00005
l=[] #list to store result after computing number of rabbits and
foxes
def bunnies(rabbits,foxes,years): #method definition
for i in range(years): #For given number of years
rtemp=rabbits+math.floor(rabbits*(A-B*foxes)) #compute number of
rabbits using given formula
ftemp=foxes-math.floor(foxes*(G-S*rabbits)) #compute
number of foxes using given formula
rabbits=rtemp #Update number of rabbits and foxes
foxes=ftemp
return [rtemp,ftemp] #return list that has number of rabbits and
foxes
if __name__=="__main__": #main method definition
print("Welcome to Predator-Prey Model.") #print on console
print("Enter Initial Rabbit Population:")
rabbits=int(input()) #read number of rabbits from user
print("Enter Initial Fox Population:")
foxes=int(input()) #read number of foxes from user
print("Enter Number of Years to Simulate:")
years=int(input()) #read number of years from
user
l=bunnies(rabbits,foxes,years) #call function to find number of
rabbits and foxes
print("After "+str(years)+" years there will be "+str(l[0])+"
rabbits") #print result
print("After "+str(years)+" years there will be "+str(l[1])+"
foxes")
Screenshots:
The Screenshots are attached below for reference.Please follow them for proper indentation.
Get Answers For Free
Most questions answered within 1 hours.