【问题标题】:python format error saying index out of range when it isntpython格式错误说索引不在范围内
【发布时间】:2021-03-07 20:36:22
【问题描述】:

我似乎遇到了问题 pythons .format() 工具。我目前有这个代码:

varno=6
input=product([True,False],repeat = (varno))
string= ({1} or not {2} or not {5} )and (not {1} or {6} )and (not {2} or not {3} )and ({3} or not {4} )and (not {4} or {5} or not {6} )
for i in input:
   print(i)  //prints (True, True, True, True, True, True)
   ans=(string.format(*i))

它会返回这个错误

    ans=(string.format(*i))
IndexError: Replacement index 6 out of range for positional args tuple

即使 i 中有 6 个值,所以 6 不应该超出范围。我不知道出了什么问题或如何解决它。任何帮助将不胜感激。

【问题讨论】:

    标签: python loops indexing format


    【解决方案1】:

    列表的索引从 0 到 n-1,因此 6 项列表中的最后一项的索引 =5,列表中的第一项的索引 =0

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 2015-02-23
      • 2016-04-17
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      相关资源
      最近更新 更多