gpt4 book ai didi

python - 连接4水平胜利检查错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:44 25 4
gpt4 key购买 nike

几天来我一直在尝试解决这个问题...这是代码...

def checkhor(array):
for x in range(1, 8):
if (array[x] == "#") or (array[x] == "@") and (array[x] <= 4):
if (array[x] == array[(x + 1)]) and (array[x] == array[(x + 2)]) and (array[x] == array[(x + 3)]):
if turn == 0:
print "You Won!"
return True
else:
print "You Lost..."
return True

...这是错误

File "/home/firearrow5235/PycharmProjects/Connect 4/Connect 4.py", line 132, in <module>
if checkhor(f):
File "/home/firearrow5235/PycharmProjects/Connect 4/Connect 4.py", line 77, in checkhor
if (array[x] == array[(x + 1)]) and (array[x] == array[(x + 2)]) and (array[x] == array[(x + 3)]):
IndexError: list index out of range

重点是,这个错误弹出的次数确实是零星的……我可以跑几轮将棋子放置在七个插槽中的任何一个中都没有问题...我将把我最后的输入放入...

['A', '#', 'O', 'O', 'O', 'O', 'O', 'O']
['B', '@', 'O', 'O', 'O', 'O', 'O', 'O']
['C', '@', 'O', 'O', 'O', 'O', 'O', 'O']
['D', '#', 'O', 'O', 'O', 'O', 'O', 'O']
['E', '@', '@', 'O', 'O', 'O', 'O', 'O']
['F', '@', '#', '#', 'O', '#', '#', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']
Enter the column you'd like to drop your piece in4

我还会添加一 block 板,表明问题不在于该部分的位置

Enter the column you'd like to drop your piece in5
['A', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['B', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['C', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['D', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['E', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
['F', 'O', 'O', 'O', 'O', '@', 'O', 'O']
[' ', '1', '2', '3', '4', '5', '6', '7']

最佳答案

我相信这与您的条件有关:

if (array[x] == "#") or (array[x] == "@") and (array[x] <= 4):

如果我正确理解你的代码,最后的检查应该是确保你没有超出 x >= 4 的范围。 ,但你有array[x] >= 4相反。

此外,您可能需要在 or 周围添加一些额外的括号。 :

>>> True or False and False
True
>>> (True or False) and False
False

其中任何一个都可能有助于通过条件,然后得到你的 IndexError当您尝试索引 x+3

关于python - 连接4水平胜利检查错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com