我正在做一個捕食獵物模擬,打印在一定時期內的獵物和捕食者的數量。使用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
我不確定你打算如何使用你的所有數字,但是我的頭頂是你的(1 + a - b * predator_population)中的舍入問題或操作順序問題,以及這樣。 – thumbtackthief 2015-02-06 22:46:23
清理你的代碼,任何可能有人會很高興檢查你的算法。但是你不能期望在這裏獲得幫助。 *你認爲什麼是錯誤的?期望輸出和實際輸出之間的差異會隨着迭代次數的增加而增加,所以您在算法中犯了一個錯誤。 – 2015-02-06 22:46:29
當您更新'predator_population'的值時,您是否打算使用'prey_population'的新值而不是舊值?因爲這就是發生了什麼事。 – jwodder 2015-02-06 22:48:08