1

I'm running a population model, and the wrong numbers always come out because I'm setting the variables to new values, but then when I want to use the old variables, the loop automatically updates itself and uses the new ones.

juvenile_population = 10
adult_population = 10
senile_population = 1
juvenile_survival = 1
adult_survival = 1
senile_survival = 0
birth_rate = 2
generations = 5


counter = 0
while counter < generations:
    juvenile_population = adult_population * birth_rate
    adult_population = juvenile_population * juvenile_survival
    senile_population = (adult_population * adult_survival) (senile_population * senile_survival)
    total_population = juvenile_population + adult_population + senile_population



    print("Juvenile: ",juvenile_population)
    print("Adult: ",adult_population)
    print("Senile: ",senile_population)
    print("Total: ",total_population)
    counter += 1

A friend said to set new named variables, but then after one loop, won't you get the same problem again? I want the variables to update, but only after they've been printed, if that makes sense. Any suggestions?

2
  • The easiest way to do this is what your friend suggested: use new variables for the storage and printing and then assign them to the old variables when you are done printing. Otherwise you can use the suggestion given in the answer and do all calculations at once Commented Jun 11, 2016 at 12:54
  • Thanks for the clarification + further tips on what my friend said about the new variables. My code is working now :) Commented Jun 11, 2016 at 14:20

2 Answers 2

1

You are overwriting the existing values with new values. With Python you can merge all four lines into one like this:

juvenile_population, adult_population, senile_population, total_population = adult_population * birth_rate, juvenile_population * juvenile_survival, (adult_population * adult_survival) (senile_population * senile_survival), juvenile_population + adult_population + senile_population

This will assign all the values at once, without overwriting them first.

Sign up to request clarification or add additional context in comments.

Comments

0

Per @Selcuk, you could use variable unpacking directly, but even with nicer formatting it looks unwieldly:

juvenile_population, adult_population, senile_population, total_population = (adult_population * birth_rate,
                                                                             juvenile_population * juvenile_survival,
                                                                             (adult_population * adult_survival) (senile_population * senile_survival), 
                                                                             juvenile_population + adult_population + senile_population)

My suggestion would be to either write a helper function, and keep "like" values in a dictionary like so:

populations = {'juvenile':  10,
               'adult':     10,
               'senile':    1
               }
survivals = {'juvenile':    1,
             'adult':       1,
             'senile':      0}
birth_rate = 2
generations = 5

def update_population(pops):
    juvie = pops['adult'] * birth_rate
    adults = pops['juvenile'] * survivals['juvenile']
    seniles = pops['adult'] * survivals['adult'] + (pops['senile'] * survivals['senile'])
    return {k:v for k,v in zip(['juvenile','adult','senile'],[juvie,adults,seniles])}

counter = 0
while counter < generations:
    populations = update_population(populations.copy())
    total_population = sum(populations.values())



    print("Juvenile: ",populations['juvenile'])
    print("Adult: ",populations['adult'])
    print("Senile: ",populations['senile'])
    print("Total: ",total_population)
    counter += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.