这是我第一次发帖,如果有任何问题请告诉我。
我正在实施的测试信息是:
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
答案 0 :(得分:3)
在使用新值计算prey_population
之前,每次转弯都会更新predator_population
的值。这就是结果。
答案 1 :(得分:1)
我发现了问题,是的,它正在四舍五入。等式中的int是四舍五入的答案。我将它更改为浮动并在print语句中舍入。我非常感谢大家的帮助。
答案 2 :(得分:0)
我不认为这有资格作为答案,但要评论的时间太长,所以"practicality beats purity"。
您要求代码执行的操作是:
猎物的新数量等于:
捕食者的新数量等于:
这是它应该做的吗?
(1)此时新号码尚未向下舍入