【问题标题】:Why does this while loop not execute under the if statement?为什么这个while循环在if语句下不执行?
【发布时间】:2019-07-07 17:04:42
【问题描述】:

我是 python 和 stackoverflow 的新手。我正在尝试通过从事小型项目来学习

项目是关于人口的:2014年中国人口约为13.7亿,并以每年0.51%的速度增长。 2014年印度人口约为12.6亿,并以每年1.35%的速度增长。确定印度人口何时超过中国。假设2014年的增长率将继续

我使用的代码是:

year = 2014
population_chn = eval(input("Enter the initial China population:")) #1.37
population_ind = eval(input("Enter the initial Indian population:")) #1.26
while population_chn == 1.37:
    population_chn += 0.0051*population_chn
    year +=1
while population_ind == 1.26:
    population_ind += 0.0135*population_ind
    year +=1
if population_ind>population_chn:
    print("Indian population will exceed china population in",year)

但是,当我运行它时没有输出。我错过了什么? 我想要的输出是

印度的人口将在 2025 年超过中国的人口。

【问题讨论】:

  • 不要使用eval()。使用float() 将输入转换为数字。
  • 运行两个完全独立的循环没有任何意义。如果您要模拟向前迈进的一年,则需要每次同步更改两个总体。现在,您每模拟一年将year 增加两次,并且不能保证您对两个总体中的每一个都执行相同数量的增量。
  • 不应该是< 而不是==,例如while population_chn < 1.37:
  • 每个 while 循环最多运行一次,因为您更改了循环中的总体变量,并且只有在您输入这些确切数字时它们才会运行。
  • 正如@CharlesDuffy 所说,您应该比较while population_ind < while population_chn 并在循环中增加两者。

标签: python function if-statement while-loop


【解决方案1】:

在你的情况下,population_ind 永远不会比 population_chn 更重要,这就是为什么你不会打印一些东西的原因

如果你想解决问题,你可以试试:

population_a = 1.37
population_b = 1.26

growing_rate_a = 0.0051
growing_rate_b = 0.0135

year = 2014
while population_a > population_b:
    year += 1
    population_b += population_b * growing_rate_b
    population_a += population_a * growing_rate_a

print(year)

【讨论】:

  • 我不知道你为什么被否决,这是一个很好的答案。
【解决方案2】:

在 while 循环之前不需要 if 语句。您可以将所有这些逻辑放入 while 循环中,并像这样更新每年的人口参数:

year = 2014
print("Enter the initial China population:")
population_chn  = eval(input())
print("Enter the initial Indian population:")
population_ind = eval(input())

while population_ind < population_chn:
    population_chn += 0.0051*population_chn
    population_ind += 0.0135*population_ind
    year +=1 
else:
    print("Indian population will exceed china population in", (year))

使用 1.37 表示 Chn 人口和 1.24 作为 ind 人口的输出:

Indian population will exceed china population in 2025

您的 if 语句从不执行的原因是您的 while 语句仅在总体等于初始输入时才执行。看看这些行:

while population_chn == 1.37:
    population_chn += 0.0051*population_chn
    year +=1
while population_ind == 1.26:
    population_ind += 0.0135*population_ind
    year +=1

它们只会更新一次,因为在 1 次迭代之后,您的 if 语句不再为真,并且人口永远不会更新。因此印度人口总是少于中国人口,所以这个说法永远不会执行。

【讨论】:

  • 为什么是-1年,你什么时候给出答案?
  • 在while语句中你已经具备了得到正确答案的必要条件,不需要减1
【解决方案3】:

在 while 循环中,您正在检查 population_chn 是否与 1.37 相等,这可能对第一种情况有效,但不适用于 while 循环的任何后续迭代,因为 population_chn += 0.0051*population_chn 更改了它的值。 population_ind 也是如此。 相反,您应该在 while 循环中比较 population_chn 和 population_ind 的值,并且只使用一个循环

while (population_chn>=population_ind):
    population_chn += 0.0051*population_chn
    population_ind += 0.0135*population_ind
    year +=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2015-09-26
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多