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

Python报错求助:IndexError:替换索引1超出位置参数元组范围

问题原因与修复方案

报错根源

你的代码存在括号闭合错误,导致format()方法参数传递异常:

  • 原代码里format(worker = workers[counterW+1])的右括号提前闭合,使得day = counterD被错误当成input()函数的第二个参数(而input()仅接受一个提示字符串参数)。
  • 此时format()只收到worker这一个关键字参数,但字符串中的{day}没有对应参数匹配,Python解析时会尝试按位置索引查找第1个位置参数,找不到就抛出IndexError: Replacement index 1 out of range。

修复后的代码

hoursWorked = float(input('How many hours did worker {worker} work, on {day}'.format(worker = workers[counterW+1], day = counterD)))

只需把day = counterD移入format()的括号内,确保两个占位符{worker}和{day}都能获取到对应参数即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 02:15:36