简体   繁体   English

python 格式错误说索引超出范围时它不是

[英]python format error saying index out of range when it isnt

I seem to be having an issue pythons.format() tool.我似乎遇到了问题 pythons.format() 工具。 I currently have this code:我目前有这个代码:

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))

and It returns this error它返回此错误

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

even though there are 6 values in i, so 6 should not be out of range.即使 i 中有 6 个值,所以 6 不应该超出范围。 I have no idea what is going wrong or how to fix it.我不知道出了什么问题或如何解决它。 Any help would be greatly appreciated.任何帮助将不胜感激。

indexes of a list run from 0 to n-1, so the last item in your 6 item list would have an index =5, and the first item in your list would have an index =0列表的索引从 0 到 n-1,因此 6 项列表中的最后一项的索引 = 5,列表中的第一项的索引 = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM