为什么我的代码不起作用?请看看你是否可以帮助我

时间:2014-09-10 09:32:22

标签: python-3.x

假设一个国家A的人口大约是80 000居民的人口 年增长率为3%,人口B为20 0000居民人口,按增长率计算并计算所需年数 对于国家A的人口超过或等于B国的人口,保持费率 增长。

执行此操作后,我需要执行另一个程序来更改以前的程序,允许用户通知人口和增长率的首字母。验证输入并允许重复操作。

我做了这个表格,但没有工作......

here

years = int(input("anecesary years: "))

populA = 80000
populB = 200000

years = 0

growthA = 0.03
growthB = 0.015 

while populA > populB:
    years += 1

populA = populA + (populA * growthA)
populB = populB + (populB * growthB)
print("after %i years the country  A exceeded the country B :",years)
print("P A: ", populA)
print("P B: " ,populB)

1 个答案:

答案 0 :(得分:2)

populA = 80000
populB = 200000

while populA > populB:

存在您的问题,您的代码将无法运行,因为populA 小于 populB起始时不超过>

更改为:

while populA < populB:

当您years 0使用years = int(input("anecesary years: "))时我也会将years = 0重置为remove years = 0,我怀疑这是您想要的。

因此,您的代码应如下所示,populA = populA + (populA * growthA) etc..并确保years = int(input("anecesary years: ")) populA = 80000 populB = 200000 growthA = 0.03 growthB = 0.015 while populA < populB: years += 1 populA += populA * growthA # same as populA = populA + (populA * growthA) populB += populB * growthB print("after %i years the country A exceeded the country B :", years) print("P A: ", populA) print("P B: ", populB) 位于while循环中:

{{1}}