假设一个国家A的人口大约是80 000居民的人口 年增长率为3%,人口B为20 0000居民人口,按增长率计算并计算所需年数 对于国家A的人口超过或等于B国的人口,保持费率 增长。
执行此操作后,我需要执行另一个程序来更改以前的程序,允许用户通知人口和增长率的首字母。验证输入并允许重复操作。
我做了这个表格,但没有工作......
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)
答案 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}}