Population Growth in Python

287 Views Asked by At

I have an assignment and I can't figure out what is wrong:

Write a Python program that calculates population growth.

  • read the initial population
  • read how many generations will be calculated
  • ask if user wants to calculate another population For each generation, the population increases by 10% due to births and decreases by 2% due to deaths. Both numbers will be rounded to the nearest integer.

I can calculate the first generation, but it doesn't calculate the next generation.

while True:
  currentPopulation = int(input("\nWhat is the current population? "))
  generations = int(input("\nHow many generations do you want to wait? "))
  
  for newPopulation in range (1, (generations + 1)):
    # calculate number of births (+10%) and add to population
    births = currentPopulation + round(currentPopulation * 0.1)
    # calculate the number of deaths (-2%) and subtract deaths from population
    newPopulation = births - round(births * 0.02)
  
  printResult = print(f"\nIf population is {currentPopulation} and you wait {generations} generation(s), there will be {newPopulation} of them.")

  again = input("\n Do you want to calculate another population? (y/n) ")
  if again.lower() == "n":
    break
  elif again.lower() == "y":
    currentPopulation
  else:
    print("\nCalculating again...")
  
print("\nBye")
1

There are 1 best solutions below

0
On
while True:
    population = int(input("\nWhat is the current population? "))
    currentPopulation = population
    generations = int(input("\nHow many generations do you want to wait? "))

    for _ in range(generations):
        # calculate number of births (+10%) and add to population
        births = currentPopulation + round(currentPopulation * 0.1)
        # calculate the number of deaths (-2%) and subtract deaths from population
        currentPopulation = births - round(births * 0.02)

    print(
        f"\nIf population is {population} and you wait {generations} generation(s), there will be {currentPopulation} of them.")

    again = input("\n Do you want to calculate another population? (y/n) ")
    if again.lower() == "n":
        break
    else:
        print("\nCalculating again...")

print("\nBye")