人口增加了一倍#years

时间:2016-10-19 22:58:22

标签: python-2.7

我不知道那里有什么问题:

current_pop = 7370000000
print "So the current population is", str(current_pop)
doubled_pop = 14740000000
year_1 = 2
year = 0
while current_pop != doubled_pop:
    if current_pop != doubled_pop:
        current_pop = current_pop * 2
        year += 1
    else:
        year += 0
print year

我已经尝试过喜欢当年流行音乐的时间。但它一直给我一年= 1

1 个答案:

答案 0 :(得分:0)

你的计划中的问题是你假设人口每年翻倍!当然,在这种情况下,人口将在一年内翻倍。值得庆幸的是,我们的情况并非如此可怕。根据世界银行提供的数据,世界人口年增长率为1.182% in 2015

你的循环不必要地复杂,但更重要的是,你的测试是错误的。你(以及之前的回答)有:

while current_pop != doubled_pop:

但是这个测试通常都会失败,因为人口不太可能以一定的速度增长,而这个速度很可能是在一年中划分的初始人口的两倍。你需要使用不等式:

while current_pop < doubled_pop:

这是一个有效的程序:

current_pop = 7370000000
growth_rate = 0.01182
print "So the current population is", str(current_pop)
doubled_pop = 2 * current_pop
year = 0
while current_pop < doubled_pop:
    current_pop = current_pop + (current_pop * growth_rate)
    year += 1

print year

这是输出:

So the current population is 7370000000
59
相关问题