同时 while 循环? PYTHON

Simultaneous while loops ? PYTHON

目前下面的代码有效,但我应该有一个疾病因素和一个人口数量触发器来配合它。基本上,青少年和老年人可能会受到疾病的影响,但只有在总人口达到用户设定的 'trigger point' 时才会发生。我不知道该怎么做,就好像我在 while 循环中添加了一个额外的子句,但我可以创建另一个 while 循环来使用,但是 generation/counter 就不起作用了. 顺便说一句,代码开头的东西是和我用的class有关的东西,不用管它。

print ("This is where you run the python model")

adult_population = Gen0.adult_population
birth_rate = Gen0.birth_rate
juvenile_population = Gen0.juvenile_population
juvenile_survival = Gen0.juvenile_survival
adult_population = Gen0.adult_population
adult_survival = Gen0.adult_survival
senile_population = Gen0.senile_population
senile_survival = Gen0.senile_survival
disease = Gen0.disease
disease_trigger = Gen0.disease_trigger
generations = Gen0.generations

counter = 0


while counter < generations:

    new_juvenile_population = adult_population * birth_rate
    new_adult_population = juvenile_population * juvenile_survival
    new_senile_population = (adult_population * adult_survival) + (senile_population * senile_survival)
    new_total = new_juvenile_population + new_adult_population + new_senile_population

    print("Juveniles:" ,new_juvenile_population)
    print("Adults:" ,new_adult_population)
    print("Seniles:" ,new_senile_population)
    print("Total population:" ,new_total)

    juvenile_population = new_juvenile_population
    adult_population = new_adult_population
    senile_population = new_senile_population

    counter += 1

在你的 while 循环中,只需将说明疾病影响的代码行放在 if 块中,否则在每次迭代期间,普通人口是否会继续进行正常人口更新while 循环,但将它们放在 if 块之外。

if juvenile_population + adult_population + senile_population >= trigger:
    # account for the disease here

我猜这就是你要求的。