我已经试着解决这个问题好几天了。。。这是密码。。。你知道吗
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
我还要加一块板,说明这不是问题所在
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']
我相信这是你有条件的两件事:
如果我正确地理解了您的代码,那么最后的检查应该是确保您没有超出
x >= 4
的界限,而是使用array[x] >= 4
。你知道吗此外,您可能希望在
or
周围加上一些括号:这两种方法中的任何一种都有助于越过条件,然后在尝试索引
x+3
时获得IndexError
相关问题 更多 >
编程相关推荐