我必须为捕食者与猎物示例找到新的值。
这是我当前的代码
# Prompting user for all the inputs
A = float(input('Rate at which prey birth exceeds natural death'))
B = float(input('Rate of predation'))
C = float(input('Rate at which predator deaths exceed births without food'))
D = float(input('predator increase in the presence of food'))
popPrey = int(input('initial prey population'))
popPred = int(input('initial predator population'))
period = int(input('number of periods'))
# Finding new values of predator and prey populations
for i in range(period):
popPrey, popPred = popPrey*(1+A-B*popPred), popPred*(1-C+D*popPrey)
print "There are {%.0f} predators and {%.0f} prey after time period {%f}" %(popPred,popPrey,period)
我现在的问题是我回答的周期数始终为5。我需要使其与1,2,3,4,5相似。目前看起来像
在{5.000000}时间段之后,这里有{20}个捕食者和{700}个猎物
时间段{5.000000}之后有{20}个捕食者和{489}个猎物
在时间段{5.000000}之后,有{20}个捕食者和{341}个猎物
时间段{5.000000}之后有{20}个捕食者和{238}个猎物
时间段{5.000000}之后有{20}个捕食者和{166}个猎物
有什么想法可以解决吗?
最佳答案
您无需更改popPrey
以及popPred
,因此每次触发时具有相同的输出。
假设的修正案(不是prey
,pred
,而是popPrey
,popPred
):
for i in range(period):
# popPrey and popPred is a function of popPrey and popPred
popPrey, popPred = popPrey*(1+A-B*popPred), popPred*(1-C+D*popPrey)
print "There are {%.0f} predators and {%.0f} prey after time period {%f}" %(popPred,popPrey,i);