捕食者与猎物python程序

时间:2014-02-24 08:22:49

标签: python

我必须找到捕食者与猎物示例的新值。

这是我目前的代码

# 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。目前它看起来像 这里有{20}掠食者和{700}时间段之后的猎物{5.000000}

在时间段{5.000000}之后有{20}个捕食者和{489}猎物

在时间段{5.000000}

之后有{20}个捕食者和{341}猎物

在时间段{5.000000}之后有{20}个捕食者和{238}猎物

在时间段{5.000000}之后有{20}个捕食者和{166}猎物

我是如何解决这个问题的?

1 个答案:

答案 0 :(得分:2)

不会更改 popPrey以及popPred,因此您在每次尝试时都有相同的输出。 提出修正案(否preypredpopPreypopPred):

 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);