使用Python的捕食者 - 猎物模拟。 Python读取方程式的问题是错误的?

时间:2015-02-06 22:42:20

标签: python

我正在进行捕食者猎物模拟,在一定时期内打出猎物和食肉动物的数量。

这是我第一次发帖,如果有任何问题请告诉我。

我正在实施的测试信息是:

a= .1
b= .01
c= .01
d= .00002
prey_population = 1000
predator_population = 20
periods = 10

a = float(input("Enter the rate at which prey birth exceeds natural death: "))
b = float(input("Enter the rate of predation: "))
c = float(input("Enter the rate at which predator deaths exceed births without food: "))
d = float(input("Predator increase with the presence of food: "))

prey_population = int(input("Enter prey population: "))
predator_population = int(input("Enter predator population: "))

periods = int(input("Enter the number of periods: "))

for i in range(1, periods + 1):
    prey_population = int(prey_population * (1 + a - b * predator_population))
    predator_population = int(predator_population * (1 - c + d * prey_population))

    print("After period", i, "there are", predator_population, "predators")
    print("After period", i, "there are", prey_population, "prey")

我的信息对猎物来说是准确的,直到第6个时期,我的捕食者输出只准确到第3个时期。

我的输出是:

After period 1 there are 20 predators
After period 1 there are 900 prey

After period 2 there are 20 predators
After period 2 there are 810 prey

After period 3 there are 20 predators
After period 3 there are 729 prey

After period 4 there are 20 predators
After period 4 there are 656 prey

After period 5 there are 20 predators
After period 5 there are 590 prey

After period 6 there are 20 predators
After period 6 there are 531 prey

After period 7 there are 19 predators
After period 7 there are 477 prey

After period 8 there are 18 predators
After period 8 there are 434 prey

After period 9 there are 17 predators
After period 9 there are 399 prey

After period 10 there are 16 predators
After period 10 there are 371 prey

它应该显示的数字是:

After period 1 there are 20 predators
After period 1 there are 900 prey

After period 2 there are 20 predators
After period 2 there are 808 prey

After period 3 there are 20 predators
After period 3 there are 724 prey

After period 4 there are 21 predators
After period 4 there are 648 prey

After period 5 there are 21 predators
After period 5 there are 580 prey

After period 6 there are 21 predators
After period 6 there are 518 prey

After period 7 there are 21 predators
After period 7 there are 463 prey

After period 8 there are 21 predators
After period 8 there are 413 prey

After period 9 there are 21 predators
After period 9 there are 369 prey

After period 10 there are 21 predators
After period 10 there are 330 prey

3 个答案:

答案 0 :(得分:3)

在使用新值计算prey_population之前,每次转弯都会更新predator_population的值。这就是结果。

答案 1 :(得分:1)

我发现了问题,是的,它正在四舍五入。等式中的int是四舍五入的答案。我将它更改为浮动并在print语句中舍入。我非常感谢大家的帮助。

答案 2 :(得分:0)

我不认为这有资格作为答案,但要评论的时间太长,所以"practicality beats purity"

您要求代码执行的操作是:

猎物的新数量等于:

  • 旧的猎物数量
    • 加上此回合的新生儿(出生率*现有猎物)(1)
    • 减去此回合被杀死的猎物[(捕食率*当前捕食者)*当前捕食者] (1)
    • 将这一切都打倒了。

捕食者的新数量等于:

  • 旧的掠食者数量
    • 减去固定死亡率(死亡率*当前捕食者)(1)
    • 加上“食物分娩”[(“食物分娩率”*新数量的猎物)*当前的食肉动物] (1)
    • 围绕这一切。

这是它应该做的吗?

(1)此时新号码尚未向下舍入