I have some formulas that I have defined as functions.
I’ve then ‘named’ them using particular parameters as I then need to use the output in further formula’s which makes it easier and neater.
I now need to loop the defined function to give the outputs for each month over the year, and when I put in the name of the function into the while loop, it just gives me the one answer 12 times over, but when I put the full function into the loop it works.
Is there a way that I can loop the function by using it’s name because as my formulae are progressing, they’re getting more and more complex and the code is getting busy and confusing.
def growPOP(p, T, r, K):
#formula to calculate the new population at the end of a month.
#p = initial population, T = total initial population
#r = growth rate per time, K = carrying capacity
GrPp = p + ((p * r)*((K - T) / K))
return(GrPp)
def rt(a, b, t):
#formula to calculate growth rate for brown fish
#a & b are constants given, t = the month number
rt = a + (b*sin(((2*pi)*t)/12))
return rt
ca = 0.052
cb = 0.132
brownPOP = 19000
goldPOP = 4400
totalPOP = brownPOP + goldPOP
carryK = 104800
redcarryK = 0.998
newcarryK = (carryK*redcarryK) + (ep/10) #ep is an input figure - for now it's 0.
month = 1
brownGrowth = growPOP(brownPOP, totalPOP, rt(ca, cb, month), carryK)
while month <= 2:
print "Month ", month
print "Grown brown fish: ", growPOP(brownPOP, totalPOP, rt(ca, cb, month), carryK)
brownPOP = endbrownPOP
goldPOP = endgoldPOP
totalPOP = endtotalPOP
carryK = newcarryK
month = month + 1
So in the above loop it gives me exactly the output I want, but I really want the loop to say “print “Grown brown fish: “, brownGrowth” and still work.
There are other formulae to get brownPOP to endbrownPOP but there’s quite a few and they work, so I didn’t think they needed to be entered in to complicate things.
I think you just forgot to recalculate brownGrowth in every loop iteration?
Move the
brownGrowth = formulaline to inside while loop: