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

Python仿《60秒》游戏if语句减法操作失效问题求助

问题解决:Python中减法操作未生效的问题

你的代码核心问题是只执行了减法运算,但没有将结果赋值回变量。Seconds - 10只是计算出一个临时值,并没有修改Seconds本身的内容,所以最终打印的还是初始值60。

修正后的代码

Seconds = 60
Weapon = input("Would you like to take the axe(-5 seconds) or the shotgun(-10 seconds) (can take both if ya want)  ")
if Weapon.lower().startswith("s"):
    Seconds -= 10  # 将减法结果重新赋值给Seconds
    print("now you have a shotgun")
# 补充支持斧头的选择逻辑(兼容同时选两种的情况)
if "axe" in Weapon.lower() or Weapon.lower().startswith("a"):
    Seconds -= 5
    print("now you have an axe")
print(f"Remaining seconds: {Seconds}")

关键说明

  • Seconds -= 10 是 Seconds = Seconds - 10 的简写形式,这行代码才会真正把减法后的结果存回Seconds变量,实现数值更新。
  • 补充了斧头的判断逻辑,当用户输入包含"axe"或以"a"开头时,会扣除5秒,同时支持用户同时选择两种武器的场景(比如输入"axe and shotgun"会触发两个条件,总共扣除15秒)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 00:22:03