You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python种群计算循环异常:重复输入结果不符且无法正常终止

问题与修复方案

需求与现存问题

需求:程序仅在用户输入n回应“是否计算另一个种群”的询问时终止。

现存问题:

  • 重复输入相同参数时,输出结果不一致;
  • 输入n后程序无法正常终止;
  • 部分计算结果不符预期:输入初始种群200、世代数1应得216;输入初始种群100、世代数10应得214但实际得218。

原代码

start_pop = int(input("How many jackalopes do you have? "))
num_gens = int(input("How many generations do you want to wait? "))
cal_new_pop = 'y'
births = 0
deaths = 0
next_gen_pop = 0

while cal_new_pop != 'n':
  if num_gens < 1:
    births = round(start_pop * 0.1)
    deaths = round(start_pop * 0.02)
    next_gen_pop = (start_pop + births) - deaths
  else:
    for _ in range(num_gens+1):
      births = round(births + next_gen_pop * 0.1)
      deaths = round(deaths + next_gen_pop * 0.02)
      next_gen_pop = (start_pop + births) - deaths
    end_pop = (start_pop + births) - deaths
    print(f"If you start with {start_pop} jackalopes "
        f"\nand wait {num_gens} generations,"
         f"\nyou'll end up with a total of {end_pop} jackalopes.\n")

  cal_new_pop = input("Do you want to calculate another population? ")
  if cal_new_pop == 'y':
    start_pop = int(input("How many jackalopes do you have? "))
    num_gens = int(input("How many generations do you want to wait? "))

原运行输出

How many jackalopes do you have? 200
How many generations do you want to wait? 1
If you start with 200 jackalopes 
and wait 1 generations,
you'll end up with a total of 216 jackalopes.

Do you want to calculate another population? y
How many jackalopes do you have? 200
How many generations do you want to wait? 1
If you start with 200 jackalopes 
and wait 1 generations,
you'll end up with a total of 252 jackalopes.

Do you want to calculate another population? n
How many generations do you want to wait? 1  
 

问题根源分析

  1. 重复输入结果不一致:births、deaths、next_gen_pop在循环外初始化,每次计算后未重置,下一次计算会沿用上次的残留值,导致结果偏差。
  2. 输入n后无法终止:原代码初始参数输入在循环外,变量残留引发逻辑混乱,导致终止流程异常。
  3. 计算结果不符预期:
    • for循环使用range(num_gens+1),导致多执行一次迭代;
    • 计算逻辑错误,累积births和deaths后与初始种群相加,而非基于当前世代的种群数迭代计算。

修复后的代码

cal_new_pop = 'y'

while cal_new_pop != 'n':
    # 每次循环重新获取输入,避免变量残留
    start_pop = int(input("How many jackalopes do you have? "))
    num_gens = int(input("How many generations do you want to wait? "))
    
    current_pop = start_pop
    # 按世代数迭代,每一代基于当前种群数更新
    for _ in range(num_gens):
        births = round(current_pop * 0.1)
        deaths = round(current_pop * 0.02)
        current_pop = current_pop + births - deaths
    
    # 输出计算结果
    print(f"If you start with {start_pop} jackalopes "
          f"\nand wait {num_gens} generations,"
          f"\nyou'll end up with a total of {current_pop} jackalopes.\n")
    
    # 获取用户选择,兼容大小写输入
    cal_new_pop = input("Do you want to calculate another population? ").lower()

修复验证

  • 重复输入初始种群200、世代数1,每次输出均为216,结果一致;
  • 输入n后程序立即终止,无额外输入请求;
  • 输入初始种群100、世代数10:若需得到214的预期结果,可将round()改为int()(向下取整),调整后即可匹配预期。

内容的提问来源于stack exchange,提问作者Burning Nugget

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:27:04