我已经开始创建一个涉及名为def grow_bacteria的函数的程序。 该计划涉及获得用户的输入以估计细菌随时间的增长。 它问道:
所以,我将它放入函数grow_bacteria以及创建for循环时遇到了麻烦。
每次循环时,打印出当前的细菌数量(然后当功能完成时,它返回最终值)。计算使用公式b = B×2 ^ n其中:
b is the total number of bacteria
B is the starting number of bacteria
n is the generation
我已经写出了上面的整个作业。这是我到目前为止所拥有的;
import math
def grow_bacteria (b, s, t):
result = b * 2 ** (t//2)
b = int(input('How long does it take the bacteria to produce a new generation '))
s = int(input('What is the starting number of bacteria? ' ))
t = int(input('How much time will we wait for it to reproduce (minutes)? '))
r = b * 2 ** (t//2)
print ('After',t,'minutes(s) we would have',r,' Bacterias')
答案 0 :(得分:0)
def grow(gen_time, pop, time):
steps = time // gen_time
print("Start with a population of {}".format(pop))
for n in range(1, steps+1):
print("At T+{:>3}, we have a population of {}".format(n*gen_time, pop*(2**n)))
grow(1, 2, 3)
打印
Start with a population of 2
At T+ 1, we have a population of 4
At T+ 2, we have a population of 8
At T+ 3, we have a population of 16