生物种群增长的Python预测

生物种群增长的Python预测,python,loops,Python,Loops,投入将是: 生物的初始数量 增长率(大于1的实数) 达到此速度所需的小时数 人口增长的小时数 我有: Population = int(input("The initial number of organisms: " )) RateOfGrowth = int(input("The rate of growth (a real number > 0): " )) HrToAchieve = int(input("The number of hours it takes to achiev

投入将是:

生物的初始数量 增长率(大于1的实数) 达到此速度所需的小时数 人口增长的小时数

我有:

Population = int(input("The initial number of organisms: " ))
RateOfGrowth = int(input("The rate of growth (a real number > 0): " ))
HrToAchieve = int(input("The number of hours it takes to achieve this rate: " ))
Input_Hrs = int(input("Enter the total hours of growth: " ))

NewGrowth = 0
Passes = Input_Hrs/HrToAchieve

while Passes > 0:
    NewGrowth = (Population * RateOfGrowth)-Population
    Population += NewGrowth
    Passes -= 1

print("The total population is", Population )

新的at循环,不确定我怎么会错过一次传球 部分使用输入10,2,2,6,提供80的正确答案

但当使用100种生物,2小时内的生长速度为5,25小时内的总生长速度为5,我得到7000种
24414062500是合适的。

你可以在一行中这样做,我假设如果x的增长率在y小时内,剩下的时间少于y小时,那么就不会有任何增长

import math

ORG = int(input("The initial number of organisms: " ))
GR = int(input("The rate of growth (a real number > 0): " ))
GR_Hr = int(input("The number of hours it takes to achieve this rate: " ))
PG_Hr = int(input("Enter the total hours of growth: " ))

Growth = ORG * int(math.pow(GR, PG_Hr//GR_Hr))  # or Growth = ORG * int(GR ** (PG_Hr // GR_Hr))
使用循环编辑

Growth_using_loops = ORG
loop_counter = PG_Hr//GR_Hr # double slash // returns a integer instead of float
for i in range(loop_counter):
    Growth_using_loops = Growth_using_loops * GR


print(Growth)
print(Growth_using_loops)
输出:

The initial number of organisms: 100
The rate of growth (a real number > 0): 5
The number of hours it takes to achieve this rate: 2
Enter the total hours of growth: 25
24414062500
24414062500

你可能会在这里找到帮助。你会如何手工计算?起草一个流程图/算法,然后实施它,或者发布它,以便我们能够更好地帮助您:)组织和GR在循环中都没有变化,因此增长确实是线性的,7000的输出与您的输入看起来是正确的。例如,一开始可能有500个生物,增长率为2,生长期达到6小时。假设没有一种生物死亡,这意味着每6小时这个种群的数量就会翻一番。因此,在允许6小时的生长后,我们将有1000个有机体,12小时后,我们将有2000个有机体。您可能在loop.TY中遗漏了一条
ORG=growth
语句,但现在希望更准确地了解基本知识loops@KrisA还编辑了我的循环解决方案答案。