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

如何让Python累计求和程序在总和超过100时正常终止?

问题原因
  • 存在未定义变量错误:累加行total = a + b中的a没有提前定义,运行时会直接触发报错,无法正常完成累加计算,自然无法触发大于100的终止逻辑,将a替换为初始化的累计变量total即可修复。
修正后完整代码
c = 0
total = 0

while total <= 100:
    c = c + 1
    b = int(input("Enter a number: "))
    total = total + b

print("Sum: " + str(total))
print("Numbers Entered: " + str(c))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 15:45:03