我正在做一个捕食者-猎物模拟,打印特定时期内猎物和捕食者的数量。
这是我第一次发帖,所以如果有任何问题,请告诉我。
我正在执行的测试信息是:
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
的值。这会影响结果。
我发现了问题,是的,它是四舍五入。方程式中的int是四舍五入的。我把它改成浮点数,并在打印声明中四舍五入。我非常感谢大家的帮助。
我不认为这是一个答案,但它太长了,不能放在评论中,所以"实用胜过纯粹"。
对于每一个转弯,你要求你的代码做的是:
猎物的新数量等于:
- 旧的猎物数量
- 加上这个回合的新生儿(出生率*当前猎物)(1)
- 减去该回合被杀死的猎物[(捕食率*当前捕食者)*当前猎物](1)
- 把这一切都倒过来
捕食者的新数量等于:
- 古老的食肉动物数量
- 减去固定死亡率(死亡率*当前捕食者)(1)
- 加上"食物出生"[("食物出生率"*猎物的新数量)*当前捕食者](1)
- 把这一切都倒过来
这就是它应该做的吗?
(1)此时,新数字尚未四舍五入