我陷入困境,因为我在范围(x)循环中没有迭代。我的计划的目的是模拟在岛上彼此相互作用的狐狸和兔子,并在每天后打印出每个动物的种群。我知道方程是正确的,我遇到的问题是我的循环只会在大范围内运行一次。
我的代码:
def run_simulation():
print()
RABBIT_BIRTH_RATE = 0.01
FOX_BIRTH_RATE = 0.005
INTERACT = 0.00001
SUCCESS = 0.01
x = 0
y = 1
FOXES = eval(input("Enter the initial number of foxes: "))
print()
RABBITS = eval(input("Enter the initial number of rabbit: "))
print()
DAYS = eval(input("Enter the number of days to run the simulation: "))
print()
print("Day\t","Rabbits\t","Foxes\t")
print(0,"\t",RABBITS,"\t","\t",FOXES,"\t")
for i in range(DAYS):
RABBITS_START = round((RABBIT_BIRTH_RATE * RABBITS) - (INTERACT * RABBITS * FOXES))
FOXES_START = round((INTERACT * SUCCESS * RABBITS * FOXES) - (FOX_BIRTH_RATE * FOXES))
y = y + x
print (y,"\t",(RABBITS_START+RABBITS),"\t","\t",(FOXES_START+FOXES),"\t")
run_simulation()
如果使用500 Foxes,10000 Rabbits和1200天的示例运行,我的输出将如下所示
Day Rabbits Foxes
0 10000 500
1 10050 498
第二条输出线重复剩余的1199次。 任何帮助将不胜感激我无法弄清楚我做错了什么。
答案 0 :(得分:3)
您在开头设置了RABBITS
和RABBIT_BIRTH_RATE
。然后,在每次循环迭代中,将RABBITS_START
设置为涉及这两个数字的某个公式。您永远不会更改RABBITS
或RABBIT_BIRTH_RATE
或FOXES
或任何内容的值,因此每次执行循环时,您只需使用相同的数字再次计算相同的内容。您需要在每次迭代时更新变量的值 - 也就是说,为RABBITS
,FOXES
等设置新值。
答案 1 :(得分:2)
对我而言,最大的问题就是你所谓的“兔子/狐狸的变化”。 RABBITS_START
听起来像RABBITS
的初始计数,但事实并非如此。这就是为什么我将它重命名为RABBITS_DELTA
,因为它实际上是在计算每天兔子的变化。
我想我明白了。至少,这个行为现在更像是模拟:
def run_simulation():
RABBIT_BIRTH_RATE = 0.01
FOX_BIRTH_RATE = 0.005
INTERACT = 0.00001
SUCCESS = 0.01
x = 0
y = 1
FOXES = eval(str(input("Enter the initial number of foxes: ")))
RABBITS = eval(str(input("Enter the initial number of rabbits: ")))
DAYS = eval(str(input("Enter the number of days to run the simulation: ")))
print("Day\t","Rabbits\t","Foxes\t")
print(0,"\t",RABBITS,"\t","\t",FOXES,"\t")
count = 0
while count < DAYS:
RABBITS_DELTA = round((RABBIT_BIRTH_RATE * RABBITS) \
- (INTERACT * RABBITS * FOXES))
FOXES_DELTA = round((INTERACT * SUCCESS * RABBITS * FOXES) \
- (FOX_BIRTH_RATE * FOXES))
y = y + x
RABBITS += RABBITS_DELTA
FOXES += FOXES_DELTA
print (y,"\t",(RABBITS),"\t","\t",(FOXES),"\t")
count += 1
run_simulation()
答案 2 :(得分:1)
我会试图解释你的意思:
for i in range(1, DAYS + 1):
rabbit_delta = ... # RABBITS_START
fox_delta = ... # FOXES_START
RABBITS += rabbit_delta
FOXES += fox_delta
print(i, "\t", RABBITS, "\t\t", FOXES, "\t")
根据他人的回答编辑。 (野生刺伤不太狂野。)
请参阅BrenBarn的答案,以解释散文。
答案 3 :(得分:0)
我想这就是你的意思
def run_simulation():
print()
RABBIT_BIRTH_RATE = 0.01
FOX_BIRTH_RATE = 0.005
INTERACT = 0.00001
SUCCESS = 0.01
x = 0
y = 1
FOXES = int(input("Enter the initial number of foxes: "))
print()
RABBITS = int(input("Enter the initial number of rabbit: "))
print()
DAYS = int(input("Enter the number of days to run the simulation: "))
print()
print("Day\t","Rabbits\t","Foxes\t")
print(0,"\t",RABBITS,"\t","\t",FOXES,"\t")
for i in range(1, DAYS + 1):
RABBITS += round((RABBIT_BIRTH_RATE * RABBITS) - (INTERACT * RABBITS * FOXES))
FOXES += round((INTERACT * SUCCESS * RABBITS * FOXES) - (FOX_BIRTH_RATE * FOXES))
y = y + x
print (i,"\t",(RABBITS),"\t","\t",(FOXES),"\t")
run_simulation()
我得到的输出是
Day Rabbits Foxes
0 10000 500
1 10050 498
2 10100 496
3 10151 494
4 10202 492
天= 4