python - 在python上列出索引超出范围
问题描述
我试图用参数作为在函数内部处理的值来创建一些过程,但是有一些问题。这是我的一些代码
.
.
.
.
def pokerBruteForce(n:int, kombinasi:list, kartu:list, komposisi:list):
c = 0
i = 0
j = 0
k = 0
l = 0
m = 0
done:bool
teks:str
if n <= 5:
pass
c = 0
i = 0
for i in range(n-1):
done = False
if ((n - c) > 4) and (kombinasi[i] == False):
for j in range(i+1,(n - 1), 1):
if kombinasi[j] == False and (((n - c) - 1) > 3):
if kartu[j].bobotCorak == kartu[i].bobotCorak and kartu[j].nilai == kartu[i].nilai - 1:
for k in range((i+1),(n - 1), 1):
if kombinasi[k] == False and (((n - c) - 2) > 2):
.
.
.
.
主要代码是
cards = []
combination = [False] * 5
composition = []
createKartu(cards)
pokerBruteForce(7, combination, cards, composition)
for i in range(len(composition)):
print(composition[i],)
在尝试编译和运行程序时,出现错误,它说
Traceback (most recent call last):
File "d:\xxx\xxx\xxxx\xxxx\xxxxx", line 330, in <module>
pokerBruteForce(7, combination, cards, composition)
File "d:\xxxx\xxxxx\xxxxxx\xxxx\xxxxx", line 54, in pokerBruteForce
if kombinasi[j] == False and (((n - c) - 1) > 3):
IndexError: list index out of range
我正在尝试在 j 变量的循环中手动追溯代码,我认为我是对的。但它总是说它的错误。也许有什么解决办法?谢谢
解决方案
你有kombinasi = [False]*5
并且你尝试访问kombinasi[5]
. 请记住,列表索引从 开始0
,因此您只能访问kombinasi[4]
.
您应该j-1
在以下行中使用:
for j in range(i+1,(n - 1), 1):
if kombinasi[j-1] == False and (((n - c) - 1) > 3):
j
或从i
to迭代(n-2)
:
for j in range(i,(n - 2), 1):
if kombinasi[j] == False and (((n - c) - 1) > 3):
推荐阅读
- mysql - 使用 webpack 启动节点应用程序:error cross-env NODE_ENV=development node src
- r - 如何对数据框中的列进行排序,以便一行中的所有相同字符串都在同一列中
- excel - VBA网页抓取英超联赛表格
- sap - 在 HANA 重启后使配置参数保持不变
- java - 在字节码层面,Java 的 Class.getEnumConstants() 如何知道哪些类是枚举类?
- azure - 连接字符串中的 Eventhub 分区键
- python - 以 10 为基数的 int() 的无效文字:'' 在 Postgres 上的 Django 中,但不是 SQLite
- linux - 使用 hwlat 测量的意外高延迟峰值
- java - 如何修复找不到模块:spring.web 和类似错误
- salesforce - 如何在合作伙伴社区中实施 Docusign