python - 使用 Python 进行捕食者-猎物模拟。 Python 读取等式错误的问题?

标签 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

最佳答案

在使用新值计算predator_population 之前,每个回合您都会更新prey_population 的值。这会影响结果。

关于python - 使用 Python 进行捕食者-猎物模拟。 Python 读取等式错误的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376099/

相关文章:

python - 滚动总和然后使用此滚动总和获得随机 int

python - 如何使用 Pandas Dataframe 重复滚动减法

python - 在 Raspberry Pi 中使用 OpenCV 和套接字通过 TCP 发送视频

python - 使用带有 Flask 的 Authlib 获取和存储刷新 token

python - 在 Docker 中运行的 uwsgi 找不到初始化文件

python - 从 Docker 启动 uwsgi

python - 是否可以将变量注入(inject)python logger Filter

python - python装饰器中的 self

即使没有定义运算符,Python 比较不相关的类型也会成功

python - Azure Eventhub (Python) : checkpointing with blob storage - keyerror issue in EventProcessor when checkpointing is enabled