How do I display population growth?

1.3k views Asked by At

Can someone help me with this code? I am using Python 3 and this is a beginner level course.

Statistics Canada projects population based on the following assumptions:

  • One birth every 78 seconds
  • One death every 105 seconds
  • One new immigrant every 147 seconds

My assignment:

  1. Write a program to display the population for each of the next ten years (i.e. 1 to 10 years from now). Assume the current population is 38,233,484 and one year has 365 days.
  2. Now rewrite the program to prompt the user to enter the number of years and displays the population after that many years. Your program will not accept a negative number.

All I could figure out till now is this:

mylist= [1,2,3,4,5,6,7,8,9,10]

for x in mylist:

    print("Year", x, "has a population of",r)

def population(b, d, i):

    p=(b+i-d)

    return p

a=(1/78)

b=(1/105)

c=(1/147)

r=population (a,b,c)
2

There are 2 answers

0
Enrique Lliulla On

Anwser 1:

import math
current_population=38233484
seconds_per_day=3600*24
days_per_year=365
seconds_per_birth=78
seconds_per_death=105
seconds_per_inmigrant=147
births_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_birth)
deaths_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_death)
inmigrants_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_inmigrant)
for years in range(10):
    new_population=current_population+(births_per_year+inmigrants_per_year-deaths_per_year)*(years+1)
    print("Year",years+1,"has a population of",new_population)

Answer 2:

import math
def input_years():
    user_input=input("Enter a positive number of years: ")
    return int(user_input)

current_population=38233484
seconds_per_day=3600*24
days_per_year=365
seconds_per_birth=78
seconds_per_death=105
seconds_per_inmigrant=147
births_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_birth)
deaths_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_death)
inmigrants_per_year=math.trunc(days_per_year*seconds_per_day/seconds_per_inmigrant)
years=input_years()
if years <0:
    print("Sorry, number of years must be positive")
else:
    new_population=current_population+(births_per_year+inmigrants_per_year-deaths_per_year)*(years)
    print("After",years,"years new population is",new_population)
2
עילאי אלמלם On

inregrations

Firebase

Run a cloud function that will run let's say every 78 seconds, one that runs every 105 and another that runs every 147 seconds that will increment/decrement total population count, respectively.

PHP/Laravel

Same as firebase, but with a CronJob.

python

This is a bit tricky because cloud functions are really hard to setup with python, so you will have to deal with a python cloud function, or pull the population count in the client-side and run an interval that will increment/decrement the population count.

TL;DR

Use firebase with cloud functions.