2014-02-24 50 views
0

我必須找到捕食者對獵物示例的新值。捕食者vs獵物蟒蛇程序

這是我當前的代碼

# Prompting user for all the inputs 
A = float(input('Rate at which prey birth exceeds natural death')) 
B = float(input('Rate of predation')) 
C = float(input('Rate at which predator deaths exceed births without food')) 
D = float(input('predator increase in the presence of food')) 
popPrey = int(input('initial prey population')) 
popPred = int(input('initial predator population')) 
period = int(input('number of periods')) 

# Finding new values of predator and prey populations 
for i in range(period): 
    popPrey, popPred = popPrey*(1+A-B*popPred), popPred*(1-C+D*popPrey) 
    print "There are {%.0f} predators and {%.0f} prey after time period {%f}" %(popPred,popPrey,period) 

我現在的問題是,我對我的回答期數總是5.我需要它像1,2,3,4,5。目前,它看起來像 這裏{20}天敵和{700}獵物一段時間後{5.000000}

有{20}天敵和{489}獵物後時間段{5.000000}

有{ 20}天敵和{341}獵物時間段{之後5.000000}

有{20}天敵和時間段{5.000000}

有{20}天敵和{166}獵物後{238}獵物經過一段時間{5.000000}

任何想法如何解決?

+0

爲什麼會產生輸出變化?您在代碼中的哪個位置嘗試更改它? – Bach

回答

1

不改變popPrey以及popPred等你在每個itteration相同的輸出。 應該修正案(無preypredpopPreypopPred):

for i in range(period): 
    # popPrey and popPred is a function of popPrey and popPred 
    popPrey, popPred = popPrey*(1+A-B*popPred), popPred*(1-C+D*popPrey) 
    print "There are {%.0f} predators and {%.0f} prey after time period {%f}" %(popPred,popPrey,i); 
+0

現在我得到了popPrey和popPred的正確值。我的最後一個問題是,時間段後有{20}個捕食者和{700}捕食者{5.000000} {20}捕食者和{489}時間段後的獵物{5.000000} 有{20} }時間段後的獵物{5.000000} 時間段後有{20}個捕食者和{238}獵物{5.000000} 時間段後有{20}個捕食者和{166}獵物有沒有辦法讓我可以認爲這個時期對所有人來說都不是5,而是對第一個來說是1,並且對於最後一個來說一直增加到5。 – user3345646

+0

如此處所述:http://stackoverflow.com/questions/21983011/predator-vs-prey應該更正此答案 – jonrsharpe