如何显示人口增长?

有人可以帮助我使用此代码吗?我正在使用Python 3,这是初级课程。

加拿大统计局根据以下假设预测人口:

  • 每78秒生一胎
  • 每105秒有1人死亡
  • 每147秒就有一名新移民

我的任务:

  1. 编写一个程序以显示未来十年(即从1到10年)的人口 现在)。假定当前人口为38233484,一年有365天。
  2. 现在重新编写程序以提示用户输入年份并显示 多年之后的人口。您的程序将不会接受负数。

到目前为止,我只能弄清楚的是:

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)
mfj111071 回答:如何显示人口增长?

答案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)

答案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)
,

禁忌

火力基地

运行一个云功能,假设它每78秒运行一次,每105秒运行一次,而每147秒运行一次,这将分别增加/减少总人口数。

PHP /拉威尔

与firebase相同,但具有 CronJob

蟒蛇

这有点棘手,因为使用python确实很难设置云函数,因此您将不得不处理python云函数,或者在客户端提取填充计数并运行一个会递增/递减的间隔人口计数。

TL; DR

使用具有云功能的Firebase。

本文链接:https://www.f2er.com/1454481.html

大家都在问