Python - While loops simple -




i trying write function calculates number of days species of bacteria takes initial population reach final population given each day population doubles. code is;

def num_doublings(initial_population, final_population):    time = 0    while initial_population < final_population:        time+= 1        initial_population = initial_population * 2        print(time) 

this prints each day, want print total number of iterations takes while loop stop. output of code when function called num_doublings(2, 8) is

1 2 3 

but need output 3. how fix ?

you can try this:

def num_doublings(initial_population, final_population):     time = 0     while initial_population <= final_population:         time+= 1         initial_population = initial_population * 2      return time 

the function return time variable 3 in example gave





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -